Search Unity

Nested prefab's transform.root returns an incorrect value

Discussion in 'Prefabs' started by F-R-O-S-T-Y, Aug 8, 2019.

  1. F-R-O-S-T-Y

    F-R-O-S-T-Y

    Joined:
    Mar 22, 2013
    Posts:
    211
    Hello,

    I stumbled upon a very weird bug today. My setup gameobject setup is the following:
    A prefab, which has a nested prefab, which has 3 nested prefabs.


    Head, Left Hand and Right Hand all have a component called CopyTransform (a custom component). Whenever the VR Avatar (Remote) is instantiated, that component is enabled in certain cases and disabled in others. In my current tests, I've forced it to be enabled (copyComponent.enabled = true). What happens is that it is enabled on the Head but not on Left Hand and Right hand. They share the same code. Debugging results show that they reach the same place in code with the same values. And there are no other places where this component is toggled on or off. I got really frustrated because I did not understand what was going on.

    Then I did a debug.log with (transform.root.name). In the case of the Head object, the root is VR Avatar (Remote), as it should be. In case of hands, the root is either Left Hand or Right Hand. It's as if the nested prefabs do not understand that they are parented under VR Avatar, which is parented under VR Avatar (Remote). If I check the hierarchy in the scene view, they seem to be correct. But for some reason, the root object is different for these 3 objects even though they should not be.

    I am using Unity 2019.1.11f1 along with Mirror networking.

    Any help would be much appreciated.
    Thanks!
     
  2. F-R-O-S-T-Y

    F-R-O-S-T-Y

    Joined:
    Mar 22, 2013
    Posts:
    211
    Figured it out.

    The reason was that VR Avatar has a script, which calls the copyComponent.enable on the Head and both Hands. However, while the head reference in the inspector referenced the Head object under VR Avatar, the Left Hand and Right Hand references were set to the prefabs in the project folder. That's why, when debugging, the transform.root.name for some reason did not return VR Avatar (Remote).

    One quirk though. I tried logging transform.parent.name in OnEnable(), Awake() and Start() methods, and there the result was "correct" (VR Avatar (Remote)). However, the method where I enable the copyComponent was called between Awake and Start (invoked by the parent objects), and there, for some reason, the result was the prefab's root (Right Hand, Left Hand).