- Home /
Question by
TwitoGames · Apr 01, 2020 at 03:44 PM ·
arrayarraysgridspawning problemspickups
How to spawn GameObjects in a grid of 9
For my game, I want to make it so that a coin spawns in a grid of 9 spots and it does that every second and the player has to pick it up.
Currently, This is what I'm using to spawn in a grid by looking at some answers but it doesn't seem to be working:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnManager : MonoBehaviour
{
[SerializeField]
private Transform[] spawnPoints;
private int index;
[SerializeField]
private GameObject coin;
public bool stopSpawning = false;
[SerializeField]
private Vector3[] occupiedSpace;
void Start()
{
StartCoroutine(PointSpawner());
if (stopSpawning == true) {
StopCoroutine(PointSpawner());
}
}
void Update()
{
index = Random.Range (0, spawnPoints.Length);
}
IEnumerator PointSpawner() {
yield return new WaitForSeconds(0.75f);
while (stopSpawning == false) {
Spawn();
Debug.Log("Coin has spawned");
yield return new WaitForSeconds(0.75f);
}
}
private void Spawn() {
Instantiate(coin, spawnPoints[index].transform);
}
}
Comment
Your answer
Follow this Question
Related Questions
How to set parents of all objects in an array? 1 Answer
Activating Bool Array in sequence. 2 Answers
RTS Grid Initialisation 0 Answers