#Instantiate creating Empty GameObject.
1 messages · Page 1 of 1 (latest)
{
counter += increment * Time.deltaTime;
spawnObject(counter);
}
private void spawnObject(float counter){
if(rng == 0){
rng = Random.Range(0, 3);
} else {
if(counter >= rng){
counter -= rng;
spawnFromTable(rng);
rng = 0;
}
}
}
private void spawnFromTable(int r){
GameObject spawned = new GameObject();
switch(r){
case 1:
// resourceWood
spawned = allyHuman;
break;
case 2:
spawned = allyWorker;
break;
case 3:
spawned = enemySkeleton;
break;
case 4:
spawned = resourceStone;
break;
case 5:
spawned = allySoldier;
break;
case 6:
spawned = resourceGold;
break;
case 7:
spawned = enemyTrap;
break;
case 8:
spawned = resourceMetal;
break;
case 9:
spawned = allyEngineer;
break;
case 10:
spawned = enemyDragon;
break;
}
Quaternion rotation = new Quaternion(0f,0f,0f,0f);
Vector3 position = new Vector3(Random.Range(transform.localScale.x * -5, transform.localScale.x*5), (spawned.transform.localScale.y / 2 + 0.1f), Random.Range(transform.localScale.z * -5, transform.localScale.z*5));
safeSpawnFromTable(spawned, position, rotation);
}
private void safeSpawnFromTable(GameObject spw, Vector3 pos, Quaternion rot){
if(spw.name != "New Game Object"){
Instantiate(spw, pos, rot);
}
}```
this is my code.
after a while, this is what I get. Every time it spawns anything, the empty GameObject that's supposed to be overriten spawns too.
Instantiate method returns the cloned object, so you can get the reference from that
If I understood this whole thing correctly
I probably didnt
This is the problem line
GameObject spawned = new GameObject();
when you use new GameObject() it spawns an empty game object
Changing it to GameObject spawned = null; should cause it to work
First, you create a new GameObject and assign it's reference to spawned
spawned = new GameObject(); ```This will spawn an empty game object. Then, based on the switch, you assign ` spawned ` to another GameObject and later on use Instantiate ` to spawn that. So in the end, you spawn two different objects . . .