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:
Setting the parent of a transform which resides in a prefab instance is not possible.
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
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!
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.
using UnityEngine;
public class SetParentToNull : MonoBehaviour
{
private void Awake()
{
transform.SetParent(null);
}
}
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.
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!
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.
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.
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.
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”
@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.
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).
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()
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.
@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.