|
Post by mattsoul on Oct 22, 2019 16:26:28 GMT
if (Input.GetKeyDown(KeyCode.Mouse0))
{
PoolBoss.SpawnInPool(obj.transform, Vector3.zero, Quaternion.identity);
obj.GetComponent<SpriteRenderer>().color=Color.red;
obj.gameObject.transform.SetParent(Camera.main.gameObject.transform);
}
the color works right; but whean i want to creat a gameObject and set it's parent unity will got a error
Setting the parent of a transform which resides in a Prefab Asset is disabled to prevent data corruption (GameObject: 'Cube').
i check the reason for this must instantiae the gameobject before setParent; but the spawnInPool seem dont't like Instantiate() method return a gameObject
how can i get the gameObject that i Spawn like Gameobject obj=PoolBoss.SpawnInPool(...);
you know , get that obj i spawn;
thank you~~~~
|
|
|
Post by DarkTonic Dev on Oct 23, 2019 3:59:08 GMT
Your code looks wrong. Try this. You should be operating on the thing you spawned, not the game object running the script, unless I'm mistaken what you're trying to do. You're trying to set the parent of what you spawn, right?
var spawned = PoolBoss.SpawnInPool(obj.transform, Vector3.zero, Quaternion.identity); spawned.GetComponent<SpriteRenderer>().color=Color.red; spawned.SetParent(Camera.main.gameObject.transform);
If that still doesn't work, Unity disabled this functionality when they did the nested prefabs stuff in 2018.3, nothing I can do to fix it. You may want to ask on the Unity forums. If you do, please send me a link so I can subscribe because I'm interested too.
|
|