Search Unity

Strange and unexpected behaviour with Transform.SetParent

Discussion in 'Scripting' started by angel_m, Jun 16, 2020.

  1. angel_m

    angel_m

    Joined:
    Nov 4, 2005
    Posts:
    1,160
    I have noticed that when assigning a parent to a gameobject with the instruction Transform.SetParent, in runtime, the new parent is rotated depending on the child gameobject rotation.
    Is it a bug?
    In any case it is an absurd and unexpected behaviour, because I expected the child to be affected by the parent transform but not inversely.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    The new parent should not be affected, no. Are you certain there is no other process acting on the parent? Can you post the code that is causing the issue?
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    If you can provide an isolated example of this happening, it definitely sounds like a bug. Seems unlikely though.
     
  4. angel_m

    angel_m

    Joined:
    Nov 4, 2005
    Posts:
    1,160
    I have to admit my game project is quite complex. But I have managed to isolate the issue and it only happens when I use an script to instantiate runtime enemy ai bots and I parent them to the root scenary object area.
    So I think the issue can be reproduced using the SetParent instruction runtime with a child moving object newly created, in my case the enemy AI bots. Then the parent transform is affected by that child and strangely the root scenary object is rotated (even although it is marked as static) o_O
     
    Last edited: Jun 16, 2020
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    There's still got to be some other factor. That is one of the most common things done in nearly every Unity game, and literally every game would break if that caused the parent to move.
     
    Kurt-Dekker likes this.
  6. angel_m

    angel_m

    Joined:
    Nov 4, 2005
    Posts:
    1,160
    I would like to share my project with you to show this strange issue, but it weights 40 Gb...
    Thanks anyway. :)
     
  7. That is not isolation. Isolation is when you open a new, empty project, drop two gameobjects in it, write a script to simulate your basic premise: moving object parenting to the not moving one and check if the rotation is changing or not. Who knows what the pathfinding/AI system does for example?
     
    Kurt-Dekker and StarManta like this.
  8. angel_m

    angel_m

    Joined:
    Nov 4, 2005
    Posts:
    1,160
    Sorry, I only wanted to say that when I don't use that script the issue does not happen.
    Thanks.
     
  9. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Hmm maybe share that particular script then? Maybe we can find an issue that you missed.
     
    Lurking-Ninja likes this.
  10. angel_m

    angel_m

    Joined:
    Nov 4, 2005
    Posts:
    1,160
    This is the script simplified, deleting the line :pirate1.SetParent(transform.root) the issue doesn't happen.The script is attached to a gameobject inside the object root (scenary) hierarchy.

    (Sorry by the spaghetti code)

    void Update()

    cam=Camera.main.transform;

    dist=Vector3.Distance(cam.position, transform.position);

    if ( dist<100){

    if (created1==false && Vector3.Distance(cam.position, point1.transform.position)<50){
    if (pirate1==null){
    pirate1=Instantiate(pirateprefab,point1.transform.position, Quaternion.identity);
    pirate1.SetParent(transform.root); // TRANSFORM.ROOT IS THE SCENARY ROOT OBJECT ***
    }else{
    pirate1.transform.position=point1.transform.position;
    pirate1.gameObject.SetActive(true);
    }
    created1=true;
    }
    }

    if ( dist>200){
    if (pirate1)
    pirate1.gameObject.SetActive(false);
    created1=false;
    }

    }