Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Rotate Child Object

Discussion in 'Editor & General Support' started by the_terrible, Apr 10, 2019.

  1. the_terrible

    the_terrible

    Joined:
    Nov 4, 2015
    Posts:
    40
    Hi all,

    I'm having trouble manipulating a child object's rotation from its parent object's script. I found a few posts regarding this issue but I still don't seem to be able to get it working correctly. I've writing the code in several ways but none seem to be working; here is my latest iteration:

    Code (csharp):
    1.  Quaternion rotatex;
    2. void Awake()
    3.     {
    4.         rotatex = GetComponentInChildren<Transform>().rotation;
    5.         rotatex.x = 30;
    6.     }
    This is part of a script attached to a parent object "Player" which has a child object with a transform and sprite image. I'm trying to change the rotation of that sprite without effecting the rotation of the parent object.
    Thanks!
     
  2. the_terrible

    the_terrible

    Joined:
    Nov 4, 2015
    Posts:
    40
    Probably better to provide an example using Euler Angles:
    Code (csharp):
    1. Vector3 rotatex;
    2. void Awake()
    3.     {
    4.         rotatex = GetComponentInChildren<Transform>().rotation.eulerAngles;
    5.         rotatex = new Vector3 (30, 0, 0);
    6.     }
     
  3. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,367
    Vector3 is not a reference type so you need to assign a new value to rotatex and then assign the child's rotation to rotatex.

    Edit: or you can skip rotatex and just do
    Code (csharp):
    1.  
    2. GetComponentInChildren<Transform>().rotation.eulerAngles=new Vector3(0f,30f,0f);
    3.  
    or whatever.
     
    Last edited: Apr 10, 2019
  4. the_terrible

    the_terrible

    Joined:
    Nov 4, 2015
    Posts:
    40
    When I use the line of code you provided I get an error CS1612:
    "Cannot modify the return value of 'Transform.rotation' because it is not a variable"

    So in that case I then tried to set a new variable for "GetComponentInChildren.rotation.eulerAngles", then setting that new variable equal to the value of rotatex, which I had already set to "30f, 0f, 0f". This did not work:

    Code (csharp):
    1. Vector3 rotatex;
    2. Vector3 rot;
    3. void Awake()
    4.     {
    5.         rotatex = new Vector3(30f, 0f, 0f);
    6.         rot = GetComponentInChildren<Transform>().rotation.eulerAngles;
    7.         rot = rotatex;
    8.     }
    I'm not sure what the issue is when you say "Vector3 is not a reference type" so I tried setting the variable "rot" as a Transform. This also returns the same CS1612 error:
    Code (csharp):
    1. Vector3 rotatex;
    2. Transform rot;
    3. void Awake()
    4.     {
    5.         rotatex = new Vector3(30f, 0f, 0f);
    6.         rot = GetComponentInChildren<Transform>();
    7.         rot.rotation.eulerAngles = rotatex;
    8.     }
     
  5. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,367
    Oh, try just
    GetComponentInChildren<Transform>().eulerAngles
    instead of
    GetComponentInChildren<Transform>().rotation.eulerAngles
     
  6. the_terrible

    the_terrible

    Joined:
    Nov 4, 2015
    Posts:
    40
    Thanks! It's working now. I'm kind of curious to know why the values in the inspector aren't changing, though.
     
  7. the_terrible

    the_terrible

    Joined:
    Nov 4, 2015
    Posts:
    40
    Wait, no, false celebration. The parent is rotating, not the child. That's why I didn't see it in the inspector. Why is the parent rotating?