I recently started getting a "NullReferenceException: Object reference not set to an instance of an object" error on my object spawning script and I have no idea why. This hasn't happened before and the script worked fine before today. Anyone know what the problem is?
Heres my script:
public var timer : float = 0.0;
public var times : float = 0.0;
var spawning : boolean = false;
var index : int;
//var cars : Rigidbody[] ;
var powerups : Rigidbody[];
var objects : Rigidbody[];
var f1 : Rigidbody;
var powerup1 : Rigidbody;
var object1 : Rigidbody ;
var coin : Rigidbody;
var spawn1 : Transform;
var spawn2 : Transform;
var spawn3 : Transform;
public static var picked : int = 0;
var timerspawn1 : int = 0;
var timerspawn2 : int = 0;
var timerspawn3 : int = 0;
public var Distance : int;
function Start(){
timec2 = PlayerPrefs.GetInt("Timec2",15);
timec3 = PlayerPrefs.GetInt("Timec3",25);
}
function Update () {
timerspawn1 += Time.deltaTime;
timerspawn2 += Time.deltaTime;
timerspawn3 += Time.deltaTime;/// So objects would have time to spawn
times += Time.deltaTime;
if(!spawning){
timer += Time.deltaTime;
}
if(timer >= 0.1){
Spawn();
}
}
function Spawn(){
spawning = true;
timer = 0;
var randomPick : int = Mathf.Abs(Random.Range(0,4));
var location : Transform;
if(randomPick == 1 && timerspawn1 > 1){
location = spawn1;
picked = 1;
timerspawn1 = 0;
}
else if(randomPick == 2 && timerspawn2 > 1){
location = spawn2;
picked = 2;
timerspawn1 = 0;
}
else if(randomPick == 3 && timerspawn3 > 1){
location = spawn3;
picked = 3;
timerspawn1 = 0;
}
var prefabs : int = Mathf.Abs(Random.Range(0,5));
if(prefabs == 1 ){
index = Random.Range (0, objects.Length);
object1 = objects[index];
object1 = Instantiate(object1, location.position, location.rotation);
object1.AddForce(Vector3(0,0,100));
}
if(prefabs == 2 && Random.value > 0.6){
index = Random.Range (0, powerups.Length);
powerup1 = powerups[index];
powerup1 = Instantiate(powerup1, location.position, location.rotation);////for powerups
powerup1.AddForce(Vector3(0,0,100));
}
if(prefabs == 2 && Random.value < 0.6){
prefabs = 1;
}
if(prefabs == 3 ){//coins//////////////////////////////////
if(times < timec2){
var thingToMakess : Rigidbody = Instantiate(coin, location.position, location.rotation);
thingToMakess.AddForce(Vector3(0,0,100));
}
}
if(prefabs == 4 && Random.value > 0.9){
yield WaitForSeconds(1);
var thingToMakerrr : Rigidbody = Instantiate(f1, location.position, location.rotation);
thingToMakerrr.AddForce(Vector3(0,0,100));
}
if(prefabs == 4 && Random.value < 0.9){
prefabs = 3;
}
//halt script for 0.3 seconds before returning to the start of the process
yield WaitForSeconds(0.3f);
picked = 0;
//set spawning back to false so timer may start again
spawning = false;
}
↧