Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Child Doesn't Rotate with Parent

Discussion in 'Scripting' started by Drakkith, May 6, 2023.

  1. Drakkith

    Drakkith

    Joined:
    Nov 29, 2016
    Posts:
    57
    Hey all. I've got a prefab dragon model that I'm instantiating that needs to be rotated by 180 degrees so it faces the camera. I can get the parent GameObject to rotate via script, but the visual model is a child of the parent and doesn't appear to rotate. In other words, I can see the rotation of the parent object in the transform of the inspector, but the visual model of the object, which is a child, doesn't rotate via the script. I can pause the editor in play mode and rotate the parent via the inspector and both it and the visual model will rotate, but if I rotate via script it doesn't seem to work. Any ideas?

    In case it's needed, here's the relevant code in the script:

    Code (CSharp):
    1. void PickupEgg()
    2.     {
    3.         if (dragonHardpoints == null) { dragonHardpoints = GameObject.Find("DragonHardpoints").GetComponent<DragonHardpoints>(); }
    4.         dragonSelectCanvas.SetActive(true);
    5.         SelectDragons();
    6.         int index = 0;
    7.         foreach (var dragon in selectedDragonsPrefabs)
    8.         {
    9.             GameObject visibleDragon = Instantiate(dragon);
    10.             visibleDragons.Add(visibleDragon);
    11.             visibleDragon.transform.position = dragonSelectSpawnPositions[index];
    12.             visibleDragon.transform.Rotate(0, 180, 0);
    13.             index++;
    14.         }
    15.     }
    I've tried rotating the object in the Instantiate() function and multiple versions of the transform.Rotate() function, but in all cases the parent rotates while the child does not.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    Does the child happen to have a Rigidbody?
    Is the child marked as static?
     
    Drakkith and Bunny83 like this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,913
    Are there any scripts attached to the dragon prefab? If so, have you checked if any of them is messing with the rotation?
     
    PraetorBlue and Drakkith like this.
  4. Drakkith

    Drakkith

    Joined:
    Nov 29, 2016
    Posts:
    57
    You guys are lifesavers. There was a couple of scripts attached that I didn't think about. Disabling them fixed the issue. Thanks so much!
     
    Bunny83 likes this.