- Home /
What is NullReferenceException :Object reference not set to an instance of an object?
What is this error and why do i keep getting it? I am very new to coding please help. Here is my code :
#pragma strict
//Player Script
//Inspector Variables
var tagName : String;
var rayDistance : float = 100.0 ; //length of ray for the raycast
var score : int = 0; // Score for the player
var gameTime : float = 20; //The time the game will last
var loadWaitTime : float = 1.0; //Amount of time to wait before next scene is loaded
var numberOfPointsToWin : int = 5;
//Private Variables
function Start()
{
InvokeRepeating("CountDown", 1.0, 1.0); //Repeat countdown every second
}
//Update is called every frame
function Update ()
{
if(Input.GetMouseButtonDown(0))
{
var hit : RaycastHit;
var ray : Ray =Camera.main.ScreenPointToRay(Input.mousePosition); //gets mouse position
if(Physics.Raycast(ray, hit, 100.0))
{
if (hit.transform.tag == tagName) {
var enemyScript = hit.transform.GetComponent(ScriptActorEnemy);
enemyScript.numberOfClicks -= 1; //Reduce the number each click
//Checks if object is at 0 for adding points to the score
if(enemyScript.numberOfClicks == 0)
{
score += enemyScript.enemyPoint; //adds points to overall score
}
}
}
}
}
function CountDown()
{ if(--gameTime == 0) //Subtract from gameTime
{
CancelInvoke("CountDown"); //Cancel CountDown at 0
if(score >= numberOfPointsToWin)
{
Application.LoadLevel("sceneScreenWin");
}
else
{
Application.LoadLevel("sceneScreenLose");
}
}
}
//
function OnGUI()
{
GUI.Label(Rect(10,10,100,20),"Score: "+ score);
GUI.Label(Rect(10,25,100,35),"Time: "+ gameTime);
}
What part of the code is causing the error? Try to only post your relevant code in the future.
It means you're trying to access an empty object. Either you didn't assign it correctly, or you're trying to access it before its assigned. Which line is throwing the error?
@jonWag Yes, its as I thought. the enemyScript variable is not getting assigned. Check my answer, see if that helps it?
If the answer helped, don't forget to mark it as correct :)
Answer by Em3rgency · Jun 24, 2013 at 06:20 PM
var enemyScript = hit.transform.GetComponent(ScriptActorEnemy);
should be
var enemyScript : ScriptActorEnemy = hit.transform.GetComponent(ScriptActorEnemy);
http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponent.html
EDIT: Woops, typos. Corrected.
The error is still occurring after I fixed the code. It still says thee problem is line 44. Any suggestions?
if (hit.transform.tag == tagName)
I don't see you actually setting the tagName anywhere. Do you do it in the inspector or what? Because otherwise, if it just defaults to the hits tagname (I don't see any other way it would pass that if check), that would mean ANYTHING the ray hits would try to get the component ScriptActorEnemy. And if not everything has it, your variable would not get set properly.
Well as a last resort, all I can think of is
if (hit.transform.tag == tagName) {
var enemyScript = hit.transform.GetComponent(ScriptActorEnemy);
if(enemyScript != null)
{
enemyScript.numberOfClicks -= 1; //Reduce the number each click
//Checks if object is at 0 for adding points to the score
if(enemyScript.numberOfClicks == 0)
{
score += enemyScript.enemyPoint; //adds points to overall score
}
}
}
Your answer is not fixing the issue. UnityScript does not require any static typing.
Your two lines in the answer do exactly the same thing.
As for the problem, is there an actual script attached to the object? Is it on the same object and not down in the hierarchy of the object?
Your answer
