Okay, so the problem here is not that the Instantiate function not working, but with me needed to have more than one carPos (the only one at the minute being -3.4, 3.4). Using these values works fine for all of my vehicles except one that is two cars side by side, as sometimes this car will overlap onto the side of my road.
Here is a photo of what Is happening. Is there anyway I can get this specific vehicle in my array to use the Pos -3.0, 3.0 or something similar to keep it within bounds?
using UnityEngine;
using System.Collections;
public class carSpawner : MonoBehaviour {
public GameObject[] cars;
public uiManager ui;
int carNo;
public float maxPos = 3.0f;
public float delayTimer;
float timer;
void Start ()
{
timer = delayTimer;
}
void Update ()
{
timer -= Time.deltaTime;
if(timer <= 0)
{
Vector3 carPos = new Vector3(Random.Range(-3.4f, 3.4f), transform.position.y,transform.position.z);
carNo = Random.Range (0,7);
Instantiate (cars[carNo], carPos, transform.rotation);
timer = delayTimer;
}
