- Home /
Why do I get an error when changing the parent of instaitaed object?
I get this error Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption. UnityEngine.Transform:set_parent(Transform) Spawner:Spawn() (at Assets/Scripts/Spawner.cs:77) Spawner:Start() (at Assets/Scripts/Spawner.cs:53)
[/Users/build/builds/unity-iphone-1.5/iphone-1.1/Projects/../Runtime/Graphics/Transform.cpp line 161]
when I do this code
public GameObject monster1;
public GameObject monster2;
public Transform monster1Holder;
public Transform monster2Holder;
.... ...
GameObject monster = monster1;
int m = 1;
if (currentMonsterRatio > Random.RandomRange(0 , 100)) {
monster = monster2;
m = 2;
}
GameObject mn = (GameObject)Instantiate(monster,SpawnPoint(),Quaternion.identity);
((Monster)mn.GetComponent(typeof(Monster))).velocity += curentSpeedIncrease;
if (m==1) mn.transform.parent = monster1Holder;
else mn.transform.parent = monster2Holder;
line 77 is the second last line of code here. Also the parent is not changed. I know this code used to work because I could see the objects in the correct place in the hierarchy. I had to change prefab I was instantiating and now I get this error.
Answer by duck · Feb 22, 2010 at 03:34 PM
What's in monster1Holder and monster2Holder? are they transforms from gameobjects in your scene, or from prefabs?
Your problem could be that if they're prefabs, you're attempting to set your newly instantiated objects as children of these prefabs (which is probably just as invalid as setting parents of prefabs!).
Thanks Yes that is what I did. I attached the wrong transforms to monster1Holder and monster2Holder.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Getting index of the smallest number in a list 1 Answer
NullReferenceException problem 2 Answers
BCE0049 error with networking script 0 Answers
Why is the script rejecting me? 1 Answer