- Home /
Only getting one game object instead of multiple game objects in start of the program
Hi I'm trying to create a 2D game where it has multiple game objects in the start of the game. But I only get one object in the game instead of the supposed no of game objects that should be in the scene.
Here's my code.
public class note_spawner : MonoBehaviour {
public GameObject noteToSpawn;
public Sprite[] noteSprites;
// Start is called before the first frame update
void Start()
{
for(int i=0;i <3; i++)
{
spawnRandomNote();
}
}
// Update is called once per frame
void Update()
{
}
public void spawnRandomNote()
{
int arrIndex = Random.Range(0, noteSprites.Length);
Sprite noteSprite = noteSprites[arrIndex];
string noteName = noteSprite.name;
GameObject newMoneyNote = Instantiate(noteToSpawn);
newMoneyNote.name = noteName;
newMoneyNote.GetComponent<note>().noteVal = int.Parse(noteName);
newMoneyNote.GetComponent<SpriteRenderer>().sprite = noteSprite;
}
}
Is there a script error during runtime? At a glance, it looks like it should logically be creating three of them, but a script error would stop it short at whatever point there was a failure.
Probably, all of them are spawning in the same coordinates since you don't give a position to spawn in Instantiate() method.
The script runs fine without any errors, and it shows that the objects are being spawned. My objective is to spawn them on top of each other but I only can see one object during runtime and I cannot figure out why the other objects aren't spawning