Search Unity

Try to setchild does not work

Discussion in 'Scripting' started by rainbow_design, Feb 14, 2016.

  1. rainbow_design

    rainbow_design

    Joined:
    Aug 8, 2015
    Posts:
    108
    Hello,
    i have two objects and try to change the parent of the one to the other.
    The parents are canvas the object is a button.

    Code (CSharp):
    1.             fmitem.transform.SetParent (g.maincanvas.transform);
    I dont get a error in unity but the button stays just in the same canvas. How can i track the problem?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    First, make sure that piece of code is actually being executed. Put a Debug.Log next to it, and see if it gets printed.

    Next, make sure it's pointing to the correct objects. You can do this like so:
    Code (csharp):
    1. Debug.Log("Setting parent of "+fmitem.gameObject.name + " to "+g.maincanvas.gameObject.name, fmitem);
    That second parameter will allow you click on the log and highlight the object passed to it.

    If those don't point to the problem then... something weirder is going on.
     
  3. rainbow_design

    rainbow_design

    Joined:
    Aug 8, 2015
    Posts:
    108
    I made sure the code is executed and addet your line:
    In the debug log i get this:

    Setting parent of Under construction to maincanvas
    UnityEngine.Debug:Log(Object, Object)
    objects:buildmapitem(String, GameObject, Vector2, String, tuf) (at Assets/scripts/execute.cs:598)
    objects:makeconstructionspace(tuf, String, maps) (at Assets/scripts/execute.cs:583)
    maps:mapclicked(String) (at Assets/scripts/execute.cs:300)
    <buildmenuitem>c__AnonStorey2:<>m__2() (at Assets/scripts/execute.cs:416)
    UnityEngine.EventSystems.EventSystem:Update()
     
  4. rainbow_design

    rainbow_design

    Joined:
    Aug 8, 2015
    Posts:
    108
    i addet one more line:


    fmitem.transform.SetParent (g.maincanvas.transform);

    Debug.Log ("parent of " + fmitem.gameObject.name + " is " + fmitem.transform.parent.name, fmitem);

    parent of Under construction is maincanvas
    UnityEngine.Debug:Log(Object, Object)
    objects:buildmapitem(String, GameObject, Vector2, String, tuf) (at Assets/scripts/execute.cs:602)
    objects:makeconstructionspace(tuf, String, maps) (at Assets/scripts/execute.cs:583)
    maps:mapclicked(String) (at Assets/scripts/execute.cs:300)
    <buildmenuitem>c__AnonStorey2:<>m__2() (at Assets/scripts/execute.cs:416)
    UnityEngine.EventSystems.EventSystem:Update()}


    But the button still shows in the uicanvas and its still in the space of the uicanvas.
     
  5. rainbow_design

    rainbow_design

    Joined:
    Aug 8, 2015
    Posts:
    108
    I found it it was reset somewhere to the old value...