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

Parent object does not rotate but child does?

Discussion in 'Scripting' started by elpuerco63, Jan 27, 2015.

  1. elpuerco63

    elpuerco63

    Joined:
    Jun 26, 2014
    Posts:
    271
    I have a column in my scene that has a child object that sits on top of the column.

    In the editor if I rotate the column it and the child object both rotate in unison.

    However in my script when I rotate the column oddly only the child object rotates?

    column.transform.Rotate(-Vector3.up * rotateSpeed * Time.deltaTime, Space.World);

    I then changed the line above to that of below to see if the column is affected and again the column remains still but the child rotates?

    column.transform.Rotate(-Vector3.left * rotateSpeed * Time.deltaTime, Space.World);

    Totally confused as the rotation is placed on the column but it only affects the child???

    Help anyone?

    Thanks
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I'm pretty certain that "column" doesn't refer to what you think it does.

    Unity acts as just as you're expecting: when you rotate an object, it and all its children rotate as one solid unit.

    So, if the code above causes only the child object to rotate, then "column" refers to the child object.

    Best,
    - Joe
     
  3. elpuerco63

    elpuerco63

    Joined:
    Jun 26, 2014
    Posts:
    271
    Your words make perfect sense.....but in the hierarchy column is the parent and the other object is beneath it. In my script I have attached the column to a publicly exposed GameObject and it is this that the rotate code runs against.

    The child object is not referred to....only the column?

    yoinks?
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Well, it doesn't matter what the objects are called in the hierarchy. Try it: using the inspector, rename one to Pickle and the other to Frankfurt. Your code will run exactly the same as it did before (but you will, perhaps, be less fooled by the names).

    So, what does matter is how you've hooked up the object references. If I understand you correctly, 'column' in your script is a public property of type GameObject. And in the inspector, you've dragged the parent object into this property slot. Is that right?

    Perhaps you can take a screen shot showing the hierarchy, and the inspector for whatever object this script is on at the same time?

    Also, can you ensure that there aren't any other scripts involved? Could you have accidentally attached the script in two places, for example?
     
  5. elpuerco63

    elpuerco63

    Joined:
    Jun 26, 2014
    Posts:
    271
    "Well, it doesn't matter what the objects are called in the hierarchy. Try it: using the inspector, rename one to Pickle and the other to Frankfurt. Your code will run exactly the same as it did before (but you will, perhaps, be less fooled by the names)."

    Agreed, but trust me I'm not fooled by the names ;-)

    "'column' in your script is a public property of type GameObject. And in the inspector, you've dragged the parent object into this property slot. Is that right?"

    Correct

    "Perhaps you can take a screen shot showing the hierarchy, and the inspector for whatever object this script is on at the same time?"

    Attached ;-)

    "Also, can you ensure that there aren't any other scripts involved? Could you have accidentally attached the script in two places, for example?"

    Definitely only in one place.

    Also, just to clarify I included this on Awake()

    column.SetActive(false);

    And as I would expect the column is hidden!

    Then when I tap the rotate button I include this:

    if (shouldRotate) {

    column.SetActive(true);

    column.transform.Rotate(-Vector3.up * rotateSpeed * Time.deltaTime, Space.World);
    }

    And as expect firstly the column is visible but again....only the child rotates

    And again just to double DOUBLE check, I removed the child altogether and run the same test and column is hidden and then shown but does not rotate?


     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Well I'm stumped. I'd like to see it for myself. Can you strip this down to a tiny project, and post it somewhere I can download? Perhaps a fresh pair of eyes will be able to spot the problem.
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    No rigid bodies involved? Rigid bodies can do weird things when parented.
     
  8. elpuerco63

    elpuerco63

    Joined:
    Jun 26, 2014
    Posts:
    271
    Well this is doing my nut in!

    I duplicated the scene and removed everything and added a cube on to the column and a script with the public gameobject linked and a single line of code in update to rotate and it does rotate!

    I'm at a loss!

    I changed the camera to a standard Unity camera replacing the Vuforia AR camera but cant see why this would make a difference?

    No rigid bodies etc.

    I even removed the entire column and recreated anew and linked to a new script with just rotate with no luck?

    The debug log says the column is rotating and as the object that is its child does rotate....what the hell??

    JoeStrout the app is too far gone for me to send it over but as your suggestion made me try the new scene approach it highlights something weird is going on in the affected scene :-(
     
  9. elpuerco63

    elpuerco63

    Joined:
    Jun 26, 2014
    Posts:
    271
    In fact oddly.....

    I just dropped a simple cube in the scene and linked that to the public game object and......it rotates! So what is sweet mercy is it about this column that wont rotate? :-(
     
  10. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Well, you're on this bug's trail now. You've narrowed it down to one corner of the woods; you know it's in there somewhere, and you just need to keep cornering it until you finally spot it and take it down.

    In other words, keep doing what you're doing: make your real object (the column) more and more like the one that works (the cube), and make your test object (cube) more and more like the one that doesn't (column).

    At some point, your working test will stop working, or your non-working case will start working, and you'll have it: the last thing you changed is what causes the issue.

    When you figure it out, please post back here... I'm mighty curious about it by this point!
     
  11. elpuerco63

    elpuerco63

    Joined:
    Jun 26, 2014
    Posts:
    271
    I'm doing just that and am totally miffed with this! I even placed the column as a child of the cube and the cube rotates but still the column does not?

    If you are interested...the column is in the free pack on Asset Store: Pillars and Columns, the blue marble one is the pain!
     
  12. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    Is the column set to static by any chance? Even if it is, I don't think that should cause what you're getting, but best to check any possibilities.

    Are there any differences at all between the column as set up in the new scene and the one in your project? Different components, different children, anything at all?

    If both objects are exactly the same and exhibit different behaviour, the problem must lie outside the object, so start bringing scripts one by one into your new scene until you get the same behaviour, and you'll have your answer.

    Well, hopefully you will. The other possibility is that you've run up against some sort of Unity bug.
     
  13. elpuerco63

    elpuerco63

    Joined:
    Jun 26, 2014
    Posts:
    271
    Well this has truly stumped me?

    I imported to the scene from that pack the column I am using, plus a greek column and a obelisk and find with changing no code the first two suffer the same problem but the obelisk does not?

    Adding a cube, cylinder etc works fine so I can only assume there is just something weird about theses prefabs?

    Not very satisfying at all and as I have wasted so much time on it now I have pulled the plug on the use of them and will have to find another way to achieve the output I'm after

    Damn it :-(
     
  14. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I'm still mighty curious. Can you zip up a tiny demo (say, one object that works and one that doesn't)?

    Seems like there must be a script or something on the prefabs that don't work that is fighting you.
     
  15. elpuerco63

    elpuerco63

    Joined:
    Jun 26, 2014
    Posts:
    271
    OK will knock one up tonight and send it to you..
     
  16. elpuerco63

    elpuerco63

    Joined:
    Jun 26, 2014
    Posts:
    271
    JoeStrout I created the sample project and bugger me if the thing rotates!! So there is something amiss in my actual project...darn!

    Do you still want the sample project (250mb) if so how can I get it to you?