Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Can't change parent of transform that resides in an instance of a prefab at runtime.

Discussion in 'Prefabs' started by chatrat12, Jun 23, 2018.

  1. chatrat12

    chatrat12

    Joined:
    Jan 21, 2015
    Posts:
    92
    I have a player prefab that has a camera rig attached to it (have not implemented a proper spawning system yet). When the game starts, I detach the camera rig from the player. I'm not able to do this any more, I get the following error:


    Code (CSharp):
    1. Setting the parent of a transform which resides in a prefab instance is not possible.
    2. UnityEngine.Transform:SetParent(Transform)
    I understand this not working when not in play mode. Do prefab instances still know they are prefabs at runtime? Should I not be able to pop objects off of my prefabs?

    There are a ton of ways around the problem I am having. Just curious as to why this constraint is in affect at runtime
     
    Last edited: Jun 23, 2018
    dadude123 likes this.
  2. runevision

    runevision

    Unity Technologies

    Joined:
    Nov 28, 2007
    Posts:
    1,432
    Hi. We can't reproduce this here. In Play Mode, we can both reparent children of Prefabs using drag-and-drop and via SetParent in script.

    Are you in Prefab Mode when the error happens? Could the error in that case come from an object from the opened Prefab in Prefab Mode, and not from objects that are part of your scenes?

    If not, then we'll appreciate a bug report with repro project and repro steps so we can reproduce it here and get it fixed. :) Write the case number here if you do that. Thanks!
     
  3. chatrat12

    chatrat12

    Joined:
    Jan 21, 2015
    Posts:
    92
    I'm able to reproduce the behavior in a new project. Prefab mode is not open. I'm using 2018 2.0x-ImprovedPrefabs.

    • Create a new GameObject,and give it a child object.
    • Turn that gameobject into a prefab.
    • Instance of the prefab is still in the scene.
    • Add script to child object that sets its transforms parent to null. This script can be placed on either the instance or the prefab itself. I used the following script.
      Code (CSharp):
      1. using UnityEngine;
      2.  
      3. public class SetParentToNull : MonoBehaviour
      4. {
      5.     private void Awake()
      6.     {
      7.         transform.SetParent(null);
      8.     }
      9. }
      10.  
    • Click Play and get the error.
     
  4. AlkisFortuneFish

    AlkisFortuneFish

    Joined:
    Apr 26, 2013
    Posts:
    430
    Out of sheer curiosity, does this happen if you do the reparenting in Start() rather than Awake()? In case some initialisation sequence is not finished yet.
     
    chatrat12 likes this.
  5. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    365
    Hmmm,

    With this script you will end up trying to change the parent inside the asset. That is why you get this error as far as I can tell.


    I believe uou need something like this
    Code (CSharp):
    1. #if UNITY_EDITOR
    2. if (EditorUtility.IsPersistent(this)
    3.     return;
    4. #endif
     
  6. runevision

    runevision

    Unity Technologies

    Joined:
    Nov 28, 2007
    Posts:
    1,432
    Ignore what Steen said (sorry Steen!). The issue is not that the script is on the asset; it will never be run there, and it also happens when it's on the instance only.

    I can reproduce the issue when the SetParent is called from Awake specifically. This is a serious bug we need to fix. Thanks for making us aware of it!
     
  7. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    365
    Right.... sorry about that, just ignore me.... Need more coffee :)
     
  8. chatrat12

    chatrat12

    Joined:
    Jan 21, 2015
    Posts:
    92
    @SteenLund It happens to the best of us!

    @runevision Glad I could help, I'll move that stuff over to the start function.
     
    runevision likes this.
  9. mmortall

    mmortall

    Joined:
    Dec 28, 2010
    Posts:
    77
    Hello. I have a similar bug in Unity 2018.3f2 while running Unit Test in EditorMode. During unit test I change prefab instance in scene.
    What is the status of this bug? Cannot find it in bug tracker. Thanks.
     
  10. runevision

    runevision

    Unity Technologies

    Joined:
    Nov 28, 2007
    Posts:
    1,432
    The issue described above was fixed long ago. The reparenting restriction should not apply in Play Mode (except if it's on an object opened in Prefab Mode), and now doesn't.

    What you describe sounds like it's by design. The restriction should apply in Edit Mode.