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!

3 Answers

3

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;

This doesn't make sense. GameObjects doesn't have a parent. GameObjects have a Transform component and these components create the hierarchy. The GameObjects are just containers.

You're right. I've edited the answer to take parent.transform instead. Thanks. My point was that he should instantiate a GameObject instead of a Transform. This is the way I do it so I know it works. According to the documentation, you can also instantiate a Transform but perhaps you can't change its parent then, I haven't tried.

No that doesn't make any difference. You can take any Component as source. Unity will always clone the whole GameObject with all components. It's just quite handy to use the component as source that you need after the instantiate. You can also use a customscript reference as source.

OK. In that case, your answer is probably correct. I'm impressed that you managed to guess the missing parts of the question :-)

I tried this also. Same error

But thx for the fast answer!

You should put that as a comment instead of an answer, otherwise it's hard to tell which answer you're referring to :-) Can you be more precise about what you're trying to do? Is @Bunny83 correct that you're actually writing an editor script and that go is actually a prefab?

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.