x


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.

more ▼

asked Feb 22 '10 at 01:32 PM

Ryuuguu gravatar image

Ryuuguu
454 13 18 29

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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!).

more ▼

answered Feb 22 '10 at 03:34 PM

duck gravatar image

duck ♦♦
41k 92 148 415

Thanks Yes that is what I did. I attached the wrong transforms to monster1Holder and monster2Holder.

Feb 23 '10 at 12:29 AM Ryuuguu
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5076
x1949

asked: Feb 22 '10 at 01:32 PM

Seen: 1597 times

Last Updated: Feb 22 '10 at 01:32 PM