Search Unity

  1. Unity 2019.1 is now released.
    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:
    93
    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,558
    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:
    93
    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:
    480
    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:
    433
    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,558
    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:
    433
    Right.... sorry about that, just ignore me.... Need more coffee :)
     
  8. chatrat12

    chatrat12

    Joined:
    Jan 21, 2015
    Posts:
    93
    @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:
    78
    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,558
    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.
     
  11. yonek_idreams

    yonek_idreams

    Joined:
    Sep 10, 2014
    Posts:
    10
    I've encounter this issue today. It is 2019.1.4f1.
    I have quite big project ("Crazy Dino Park") that after two years of production was moved from 2017 LTS to 2019.
    The issue did appear in a prefab that is loaded form Resources and instantiated, then an Animator is triggering an event that is doing a SetParent and trying to move one gameobject to another withing the instantiated prefab.

    I did hack it by making a clone of the gameobject and reparent but it would be nice to fix it.
     
  12. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    433
    @yonek_idreams

    Are you saying you use PrefabUtility.InstantiatePrefab in playmode or that you have ExecuteInEditMode scripts that modify the prefab instance?
     
  13. Kholeslaw

    Kholeslaw

    Joined:
    May 7, 2013
    Posts:
    10
    I am running 2019.1.5f1
    I have a prefab that I instantiate at runtime. That prefab has a MonoBehaviour component with a public property holding reference to another prefab - which gets instantiated shortly after the first prefab has been created. When I try to parent that latter prefab to the first prefab it results in that error.

    "Setting the parent of a transform which resides in a Prefab Asset is disabled to prevent data corruption"

    None of the prefabs are instantiated on Awake.
     
  14. Kholeslaw

    Kholeslaw

    Joined:
    May 7, 2013
    Posts:
    10
    I guess something was confusing Unity. I shut it down and reloaded the project and this issue disappeared.