- Home /
Why don't the Prefabs in my GameObject appear?
Hello, everyone. I am still learning how to use Unity. I am trying to create a Randomizer that randomly spawns a group of Peaches. I noticed that when I place the Prefabs for the Peaches in the Spawner, the Randomizer works; however, when I place the GameObjects that contain the Prefabs for the Peaches in the Spawner, the Randomizer does not work. Could someone provide a solution to the issue then explain what was causing the issue? Thank you.
The Code that I am using:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PeachSpawner : MonoBehaviour { public List spawnObjects = new List();
public float timeToSpawn;
private float currentTimeToSpawn;
public bool isTimer;
public bool isRandomized;
// Start is called before the first frame update
void Start()
{
currentTimeToSpawn = timeToSpawn;
}
// Update is called once per frame
void Update()
{
if (isTimer)
{
updateTimer();
}
}
private void updateTimer()
{
if (currentTimeToSpawn > 0)
{
currentTimeToSpawn -= Time.deltaTime;
}
else
{
SpawnObject();
currentTimeToSpawn = timeToSpawn;
}
}
public void SpawnObject()
{
int index = isRandomized ? Random.Range(0, spawnObjects.Count) : 0;
if (spawnObjects.Count > 0)
{
Instantiate(spawnObjects[index], transform.position, Quaternion.identity);
}
}
}
Answer by SamElTerrible · Oct 11, 2021 at 09:50 AM
Can you elaborate a bit on 'does not work'? Do you get any errors?