Why i'm getting exception Setting the parent of a transform which resides in a prefab is disabled ?

When i generate a buildings city i’m getting the exception.
In the Hierarchy all the objects Tile_ and Terrain_ should be children under another GameObject.

It was working fine before i when i created just cubes using CreatePrimivitbe:

prefab = GameObject.CreatePrimitive(PrimitiveType.Cube);

But now i’m using Instantiate:

prefab = map.Prefab;
Instantiate(prefab);

And when using Instantiate i’m getting the exception on the line:

prefab.transform.parent = map.transform;

Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption

What can i do to solve it ?

Try this instead:

instance = (GameObject)Instantiate(map.prefab);
instance.transform.parent = map.transform;

Instantiate returns the reference you want to use. what you’re doing is you’re trying to set the prefab itself, which is basically just a blueprint for a GameObject.

the prefab you’re likely referencing isn’t the object that you see in the scene. its a project asset, and project assets can’t reference scene assets. i.e. a prefab can’t be parented to a scene object.

That blue text you see in the heirarchy? those are prefab instances and those are scene assets. not exactly the same as a Prefab.

1 Like

Thanks working.