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:
    94
    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,615
    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:
    94
    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:
    638
    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:
    443
    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,615
    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:
    443
    Right.... sorry about that, just ignore me.... Need more coffee :)
     
  8. chatrat12

    chatrat12

    Joined:
    Jan 21, 2015
    Posts:
    94
    @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:
    81
    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,615
    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:
    11
    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:
    443
    @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:
    11
    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:
    11
    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:
    323
    @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

    Unity Technologies

    Joined:
    Nov 28, 2007
    Posts:
    1,615
    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:
    323
    Cool beans!
     
  18. VoxelMatt

    VoxelMatt

    Joined:
    Apr 22, 2015
    Posts:
    5
    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

    Unity Technologies

    Joined:
    Nov 28, 2007
    Posts:
    1,615
    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.