I have been trying to devise a way to spawn a random prefab at two different spawn points. I’m not a good coder but I managed to cobble something together by using unity answers and doing tutorials. However, the script I wrote is giving me multiple errors and I don’t understand why. Here is what I have:
using UnityEngine;
using System.Collections;
public class MainScript : Monobehaviour {
// these are global variables to be set in the inspector.
public GameObject[] CarsRight; // array of prefabs representing cars coming from the right
public GameObject[] CarsLeft; // array of prefabs representing cars coming from the left
public GameObject SpawnR; // right spawn point
public GameObject SpawnL; // left spawn point
// use this for initialization
void Start () {
int CarsR_num = Random.Range (0,3); // random number between 0-3 represents the 4 possible right car prefabs
int CarsL_num = Random.Range (0,3); // random number between 0-3 represents the 4 possible left car prefabs
InvokeRepeating ("CreateCars", 0f, 1.5f); // call and repeat function CreateCars at runtime, and there after every 1.5 seconds
}
// CreateCars is called every 1.5 seconds
void CreateCars () {
GameObject UpperCars = Instantiate (CarsRight [CarsR_num]); // adding right spawning cars to the stage
UpperCars.transform.position = SpawnR.transform.position; // setting upper car position to right spawn point
GameObject LowerCars = Instantiate (CarsLeft [CarsL_num]); // adding left spawning cars to the stage
LowerCars.transform.position = SpawnL.transform.position; // setting lower car position to left spawn
}
}