An error occurred while trying to set a child object in my GameObject in a script

GameObject enemy = Instantiate(spawnObject,spawnPosition,spawnObject.transform.rotation) as GameObject; enemy.transform.parent = transform; 

The code above generates the expected result when I test my game in game mode, however I get an error message:

"Setting the transform parent that is in the collection is disabled to prevent data corruption."

Yes, the spawnObject variable contains prefab, however creating a new GameObject should fix the problem, I suppose?

+4
source share
2 answers

Check if your variable really "convert" from the object of the game, and not from the collection.

 var transform = somePrefab.transform; enemy.transform.parent = transform; // this won't work var transform = someOtherGameObject.transform; enemy.transform.parent = transform; // this will 

Perhaps you could give more information about where your conversion variable came from.

+1
source

I also saw this problem - an instance of GameObject (rather than prefab) giving this error message. My GameObject (A) was placed in the middle of another instance of GameObject (B) of a different type. I wanted to return A to another part of B - which would fail with this error. My only solution was to first return A to zero and then re-enter B.

0
source

All Articles