Search Unity

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:
    122
    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

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    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:
    122
    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:
    973
    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:
    639
    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

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    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!
     
    Ratherjolly likes this.
  7. SteenLund

    SteenLund

    Unity Technologies

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

    chatrat12

    Joined:
    Jan 21, 2015
    Posts:
    122
    @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:
    89
    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

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    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:
    26
    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:
    639
    @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:
    35
    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:
    35
    I guess something was confusing Unity. I shut it down and reloaded the project and this issue disappeared.
     
  15. nsxdavid

    nsxdavid

    Joined:
    Apr 6, 2009
    Posts:
    476
    @runevision This appears to still be a bug as late as 2019.1.8

    I have a prefab with lots of objects in it. In play mode, if I drag this prefab to the scene I get this error when the script tries to set the transform.parent on one of the objects to null. This does not occur for any instances of the prefab already in the scene when you hit play. Just when it is already in play and you try to add another. It's a blocking issue... at least until I can find a way around it. Doing the operation in Start() vs. Awake(). vs. even Update() has same behavior.
     
  16. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    This is not really the same bug, though it touches upon the same thing. The bug originally reported in this thread was fixed.

    What you describe here is a bug related to the fact that if you drag a Prefab into a scene while in Play Mode, we don't unpack it like we do for Prefabs that were already in the scene when entering Play Mode. This is why its treated as a Prefab with the limitations that follow even though it's in Play Mode. The fix here is to unpack Prefabs when you drag them into the scene while in Play Mode. We've opened a bug case for this issue (case 1166938).
     
  17. nsxdavid

    nsxdavid

    Joined:
    Apr 6, 2009
    Posts:
    476
    Cool beans!
     
  18. VoxelMatt

    VoxelMatt

    Joined:
    Apr 22, 2015
    Posts:
    42
    I'm also getting this bug in Unity 2018.4.3f1.

    When I load into a new scene one of my scripts calls Initialise(prefab).
    And in the OnEnable() of the newly created object it reassigns the transform.parent of one of it's children.

    If I do it in the Start() function it's fine. But I need it to be OnEnable()
     
  19. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    The original issue reported in this thread has been fixed. Are you saying you can reproduce it in 2018.4 using the script and steps posted by @chatrat12 here? https://forum.unity.com/threads/can...e-of-a-prefab-at-runtime.537430/#post-3543884

    If that doesn't reproduce, but you have some other conditions that do, then please file a bug report so we can look into it.

    The only currently remaining known issue we have related to this is the issue when dragging Prefabs into the scene in Play Mode that @nsxdavid mentioned.
     
  20. Deleted User

    Deleted User

    Guest

    @runevision Can you tell since which version the bug has been fixed? I came across the following problem with Unity 2018.4.12.f1:

    I have a prefab A, with two prefab children B and C, of which C again has prefab children D - G.
    every D - G has a script referencing child gameobjects.
    I dragged A into the scene during play mode and got the error mentioned here caused by detaching a child object via child.parent == null in Awake.
     
  21. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    Which bug? Multiple bugs have been mentioned in this thread.

    The bug in the first post was found and fixed LONG ago (since I already said in a reply in January 2019 that it was fixed long ago). I can't tell you in which exact versions without a case number.

    The most recently mentioned bug about manually dragging Prefabs into the scene in Play Mode looks like it hasn't yet been fixed, except for upcoming Unity 2020.1. Backports are scheduled.
     
  22. Deleted User

    Deleted User

    Guest

    That answer is sufficient for me ;)
     
  23. dilippatil

    dilippatil

    Joined:
    Jan 4, 2015
    Posts:
    9
    the same bug is still there in unity 2019.1.3f1, much irritating. Setparent is not working..
     
  24. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    The latest version of 2019.1 is 2019.1.14, which has lots of fixes compared to the older 2019.1.3.
     
  25. Mojo-Game-Studio

    Mojo-Game-Studio

    Joined:
    Sep 18, 2015
    Posts:
    111
    Is there a workaround for this in edit mode?
     
  26. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    No, Prefab instances do not support reparenting children. There is no override type for this. You can reparent a child in the Prefab Asset itself in Prefab Mode, or you can unpack a Prefab instance and then reparent children in it, but then it has no connection to the Prefab anymore.
     
    rocky1138 and Mojo-Game-Studio like this.
  27. dissapointed

    dissapointed

    Joined:
    Aug 11, 2021
    Posts:
    1
    was this ever fixed? it appears that i still have this bug.
     
  28. moritz_jaeger

    moritz_jaeger

    Joined:
    Jun 15, 2015
    Posts:
    7
    The same problem as from the original post of this thread started appearing again for me after I upgraded from 2022.1 to 2022.2.

    - Error: "Setting the parent of a transform which resides in a Prefab instance is not possible"
    - When trying to reparent a transform inside of a prefab during runtime
    - Inside of an Awake() function.

    I created a bug report for it: IN-17572
     
    Last edited: Sep 23, 2022
  29. Mads-Nyholm

    Mads-Nyholm

    Unity Technologies

    Joined:
    Aug 19, 2013
    Posts:
    219
    Hi, I just tried to repro your issue on latest 2022.2 beta, without luck. Have you tried the latest beta build?
     
  30. rocky1138

    rocky1138

    Joined:
    Aug 13, 2012
    Posts:
    31
    Thank you for making this clear.