Object reference not set as an instant of an object
This is what it says:
NullReferenceException: Object reference not set to an instance of an object carSpawner.SpawnCar () (at Assets/carSpawner.js:28) carSpawner.FixedUpdate () (at Assets/carSpawner.js:21)
Here is my script: #pragma strict var SpawnChooser; var startTime : float = 3f; //give you a delay at the begining of the game. var timeLeft : float; var timeBetweenSpawns : float = 1.5f; //if you want to speed up the spawn rate, use this. var spawnLocation : GameObject[]; var objectToSpawn : Transform; function Awake(){ spawnLocation = new GameObject[4];// asuming that we only have 5 lanes. timeLeft = startTime; } function FixedUpdate(){ //I think the biggest problem is the lack of a timer, between spawns. if(timeLeft > 0){ %|1144764443_15|% }else if(timeLeft <= 0){ SpawnCar(); timeLeft =timeBetweenSpawns; } %|-62069431_20|% function SpawnCar(){ %|1422920987_22|% print ("Car spawn"); Instantiate(objectToSpawn,spawnLocation[laneToSpawn].transform.position,Quaternion.identity); }
My obejctToSpawn is a prefab of a car.. I get this error a lot, but it still confuses me..
Please format your script with the 1010 button. I think you didn't assign the "objectToSpawn" which by the way i think should be a gameObject ins$$anonymous$$d of transform.
as JedBeryll said you can't use intantiate from a transform, you can do two ways to fix this. first change type of the variable objectToSpawn to GameObject, in line 6,
var objectToSpawn : GameObject;
you will need to pass the entire GameObject to the script. the other way is obtain the GameObject that component is attached, in line 28
Instantiate(objectToSpawn.gameObject, spawnLocation[laneToSpawn].transform.position, Quaternion.identity);