- Home /
I fixed a problem in my code, but don't understand WHY it fixed it, explain please.
#pragma strict
var unitSelected : boolean = false;
var currentUnit : Transform;
function Start ()
{
}
function Update ()
{
var getCoords : Vector3;
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//var currentUnit : Transform;
if(Input.GetMouseButtonDown(0))
{
print(unitSelected + "!!!");
if(unitSelected == true && Physics.Raycast(ray, hit, 1000))
{
getCoords = Vector3(Mathf.Round(hit.point.x), Mathf.Round(hit.point.y), Mathf.Round(hit.point.z));
currentUnit.position = getCoords;
unitSelected = false;
}
else if(Physics.Raycast(ray, hit, 1000))
{
currentUnit = hit.transform;
//For debugging purposes, the next two lines are staying, but are not being used elsewhere in this script
getCoords = Vector3(Mathf.Round(hit.point.x), Mathf.Round(hit.point.y), Mathf.Round(hit.point.z));
print(getCoords);
if(currentUnit != null && currentUnit.tag == "Player 1")
{
print(currentUnit);
unitSelected = true;
}
}
}
}
In this code, the:
var currentUnit : Transform;
was previously being declared only in the function Update instead of at the top (what is this called?). You can see where I commented it out there, just after the ray variables were declared. My question is, why when I moved the variables declaration up from the function Update to the top, wasmy NullReferenceException suddenly gone? Previously through debugging I discovered that the currentUnit variable was being set to null right after I clicked on something.
Can anyone please break this down for me? What happens to the variable when it's being declared in function Update that doesn't happen when it's declared at the top?
Answer by Eric5h5 · Jan 10, 2013 at 07:41 AM
When a variable is declared outside a function, it's global. If it's inside Update then it's local, and is reset to null every frame because you say "var currentUnit : Transform" without assigning it a value. If it's global, then once it's set to something other than null, it stays set since it's not being re-declared every frame. So your attempt to do "currentUnit.position = getCoords" is using a variable that will always be null (unless it's global).
Answer by $$anonymous$$ · Jan 10, 2013 at 07:46 AM
What most likely happened is this:
The update function is of the course the function which is run by Unity once every frame. When you declare your variable currentUnit in the Update function it becomes a local variable which only resides within this function. This means that at the end of the function, the variable will be pushed out of what is called a stack frame, and it will be unloaded from memory. Then during the next frame, you reinitialize the variable again and so it holds no value whatsoever, and thus becomes a null reference :) Basically, whatever value you set this variable to it would be forgotten after the Update function had finished running once (not sure how you set this value beforehand, but if it was only set once a single time it would be "forgotten" again in the next frame). So if you set the value of the variable only once, it will simply be deleted and then when you reference the variable during the next frame, it would be null because the variable is remade with no value, so to speak.
On the other hand, when you declare a variable outside of any function (it can be the top of your script, but the line location doesn't matter, only if it's outside of a function or not), you are essentially making a "global" scope variable which can be seen by all functions in your script. The important difference here being that when currentUnit is declared outside of any functions, it will exist independently of those functions. This means that when you now set the value of currentUnit that value will simply exist as long as the GameObject which the script is attached to exists, it continues to live and is not influenced by the functions this way.
Hope this helps :)
Your answer

Follow this Question
Related Questions
How do I switch Canvases? & NullReferenceException (U5) 4 Answers
Variables are assigned but it returns null 2 Answers
Having a very strange problem passing a variable from one script to another 1 Answer
Is there any way to edit variables inside other scripts without declaring them as static? 1 Answer
C# NullReferenceException 1 Answer