- Home /
Object not set to a reference(again)
Apologies for this. I know doing a != and == probably is probably open to critism but im still at the early learning stages.
I have two very basic scripts and both were working until I inserted the != part and associated var's. Can you help me pinpoint what is wrong?
robo-istat.js
#pragma strict
var powerup : robo_powerup;
var Start_Player_Agility = 10;
var Start_Player_Weight = 100;
var Start_Player_Speed = 100;
var Start_Jump_Speed = 2;
var chMotor : CharacterMotor;
var player: Transform;
function Start () {
chMotor = GetComponent(CharacterMotor);
powerup = GetComponent(robo_powerup);
}
function Update () {
if (powerup.triggered == true)
{ chMotor.movement.maxForwardSpeed = 1;
print("Super Slow");
}
if (powerup.triggered != true)
{ chMotor.movement.maxForwardSpeed = Start_Player_Speed;
print ("Normal speed");
}
}
Then my second script - robo_powerup.js
#pragma strict
var destroyedpowerup : GameObject; // Declare the object to be destroyed
var target : Collider; //this is the variable that will hold the colliding object
var triggered : boolean = false; //If we only want to detect the first time it's triggered
function Start () {
}
function OnTriggerEnter(collision : Collider)
{
if (collision != target) //The colliding object isn't our object
{
return; //don't do anything if it's not our target
}
triggered = true;
print("This worked");
Destroy(destroyedpowerup);
// Print - to debug testing
// Destroy sphere
}
Many thanks in advance :-)
[EDIT] Opps forgot the error - NullReferenceException: Object reference not set to an instance of an object robo-istat.Update () (at Assets/robo-istat.js:18) [/EDIT]
try setting triggered in robo_powerup as a static variable like this:
static var triggered
Answer by Statement · Jun 07, 2012 at 08:53 PM
Object reference not set to an instance of an object robo-istat.Update ()
(at Assets/robo-istat.js:18)
function Update ()
{
if (powerup.triggered == true)
{
chMotor.movement.maxForwardSpeed = 1;
print("Super Slow");
}
if (powerup.triggered != true)
{
chMotor.movement.maxForwardSpeed = Start_Player_Speed;
print ("Normal speed");
}
}
The error you are getting, object reference not set to an instance (and it says which function and line it is), should help pinpoint the issue. I don't know which line it is since I don't have the line numbers, but double clicking the error would bring you to the line that caused the error. In this case, either powerup, chMotor or chMotor.movement is null, that is, not set to reference any object. Trying to access any member of a reference that doesn't lead anywhere will cause this error.
Your answer

Follow this Question
Related Questions
Access a Script from Another Scene? "Object Reference not set..." 1 Answer
Object reference not set to an instance of an object? C# 1 Answer
How do I pass an object to another script? 1 Answer
How to know exactly which game objects and objects are not referenced ? 1 Answer
"Object Reference not set to an instance of an object" What does it mean? 2 Answers