- Home /
"Object reference not set to an instance of an object" Error
Hi all, I am attempting to create a function, located in the main Camera object, that is called by the player object when something specific happens. The function simply inverts the value of a float in the Camera object by multiplying it against -1.
However, whenever the event happens in the game (the player passing a specific trigger) I am met with the error "Object reference not set to an instance of an object". I'm using GetComponent to attempt to run the function that is in the camera's script, but this does not seem to work (in the BeeReverse function below). The next few lines are another GetComponent that is accessing a variable from a different script on the player using the same syntax and it is working fine.
Does anyone have any idea why my attempts to run the function from the camera object are failing?
This is the player's Code:
var player : GameObject;
player = GameObject.FindWithTag("Player");
var beeCam : Camera;
beeCam = Camera.main;
// These variables are used for the player's health and health bar
var curHP : float=3;
var maxHP : float=3;
var gems : int = 0;
function Update(){
if (curHP > maxHP){
curHP = maxHP;
}
if (Input.GetKeyDown("escape")){
Application.LoadLevel(0);
}
}
function ChangeHP(Change:float) {
// This line will take whatever value is passed to this function and add it to curHP.
curHP += Change;
// This if statement ensures that we don't go over the max health
if(curHP>maxHP) {
curHP=maxHP;
}
// This if statement is to check if the player has died
if(curHP<=0) {
Application.LoadLevel(Application.loadedLevel); //restarts level
Debug.Log("Player has died!");
}
}
function BeeReverse(){
var camScript : LookAtBee = beeCam.GetComponent(LookAtBee);
camScript.Flip(); //turns little camera around!
var moveScript : CharacterControlling = player.GetComponent(CharacterControlling);
moveScript.fallSpeed = (moveScript.fallSpeed * -1);
transform.Rotate(0,180,0); //turns little bee around!
}
This is the camera's code I'm attempting to access:
var target : Transform;
public var offset : float = 326.05;
@script AddComponentMenu("Camera-Control/Smooth Look At")
function LateUpdate () {
transform.position.y = (target.position.y) - offset;
}
function Start () {
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
function Flip(){
offset = (offset * -1);
}
If it helps, I tend to find I get NullRefs etc when trying to access cameras sometimes, even though the Camera is found and the function works...it still throws the error. BIZARRRROOOOO
The error message should indicate a specific file and line number. If you know which line is a problem, you'll know which reference isn't set.
He could be experiencing the same bug as I. As I say, if this is the case it'll throw an error but work unhindered anyway. If it doesn't work at all then it's not the same bug, or even a bug at all.
It's pointing to line 39 in the player script, however I don't know what is causing the error since I don't know what's wrong with my use of GetComponent on the line above.
I copied and pasted an example from the docs and got this error. I figured I was missing something with regard to the Camera not being a regular gameobject.
I tried a lot of different things but in the end cached the Transform of the Camera in Start()
private var camera$$anonymous$$ain : Camera;
private var cameraCurrentT : Transform;
function Start()
{
camera$$anonymous$$ain = Camera.main;
cameraCurrentT = camera$$anonymous$$ain.gameObject.GetComponent(Transform);
Answer by Firedan1176 · May 29, 2014 at 12:48 AM
It should say NullReferenceException before that error. This usually means you're basically trying to do something with a variable equal to null. You can try using if(camScript) or if(camScript !=null). hope I help.
Apologies, could you elaborate on how I would implement this if statement? I'm still new to scripting and am having trouble comprehending.
Thanks for your help!
Your answer
Follow this Question
Related Questions
Getcomponent Javascript error 3 Answers
Null Reference Exception on Raycast 2 Answers
[JS] - GetComponents (No appropriate version) 1 Answer