parent a prefab

Hey guys I have another problem.

        Weapon = go.transform.GetComponent<ActualEquiped>().getEquipPlaces()[0].transform;
		
		pS = Resources.Load("ParticleSystems/BloodRushParticle") as GameObject;
		Transform toInstantiate = pS.transform;
		Transform Emitter = Weapon.FindChild("Emitter");
		instantiated = (Transform)Instantiate(toInstantiate,Emitter.position,Emitter.rotation);
		instantiated.parent = Emitter;

Every time I use this code I get this error:

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

I searched in internet for solutions and found something, but that didn’t work.(Or I did something wrong)

Now I wanna ask how I can do that, to get no Data-Corruption.

Thx for help!

Perhaps you could try working with GameObjects instead of Transforms.

...
GameObject instantiated = GameObject.Instantiate(toInstantiate, Emitter.position, Emitter.rotation) as GameObject;
instantiated.transform.parent = Emitter;

I tried this also. Same error

But thx for the fast answer!

This can only happen when you:

  • execute this code in the editor (at edit time, not at tuntime)
  • and one of your two GameObjects are a prefab and not an instance

Since you instantiate the particle gameobject, i guess the Gameobject “go” is a prefab and not an instance.

If this is an editor script you should know that working with prefabs is a bit tricky. Prefabs are an editor only feature. Prefabs do not exist at runtime. Instantiate() will create a unconnected copy. If you want to instantiate a prefab like when you drag it into the scene you have to use PrefabUtility.InstantiatePrefab.

If you want to change the prefab (update it) you have to create a normal instance (Instantiate), do your changes and then use PrefabUtility.ReplacePrefab to copy the instance back to the prefab. After that you can delete your temp-instance.