Can someone please help me? Im new to c# and have been folling tutorials mainly, it would be a big help if someone could assist me please.
This is my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RandomSpawn : MonoBehaviour {
// prefabs to instantiate
public GameObject prefab1, prefab2;
// spawn prefabs once per 2 seconds
public float spawnRate = 2f;
// variable to set next spawn
float nextSpawn = 0f;
// varaible to contain random value
int whatToSpawn;
// Update is called once per frame
void Update() {
if (Time.time > nextSpawn)
{ // if time has come
whatToSpawn = Random.Range(1, 3); // define random value between 1 and 5 (6 is exclusive)
Debug.Log(whatToSpawn);
// instantiate a prefab depending on random value
switch (whatToSpawn)
{
case 1:
Instantiate(prefab1, transform.position, Quaternion.identity);
break;
case 2:
Instantiate(prefab2, transform.position, Quaternion.identity);
break;
}
//set next spawn time
nextSpawn = Time.time + spawnRate;
}
}
}