Hi everyone, anyone ever run into this error?
“NullReferenceException: Object reference not set to an instance of an object”. I’m doing the 2D platformer tutorial and get this when running the game.
Any help is much appreciated, thanks.
Hi everyone, anyone ever run into this error?
“NullReferenceException: Object reference not set to an instance of an object”. I’m doing the 2D platformer tutorial and get this when running the game.
Any help is much appreciated, thanks.
It means that you are trying to access an object via reference that is null, ie. it has not been set.
For example, if you do this:
var mygameobject : GameObject;
mygameobject.active = false;
You will get NullReferenceException, because nothing has been assigned to mygameobject variable;
Hi KaelisAsur, thanks for the quick reply. Thanks to you I finally understand that error message. :lol:
Bump! This is driving me up the wall; I’m getting a NRE at line 18:
var scorePosition = cam.WorldToViewportPoint (follower.position) + Vector2(-.025, .01);
// This is the object to follow
var leader : Transform;
// This is the object which follows
var follower : Transform;
// This is the speed with which the follower will pursue
var speed : float = 2.0;
// This is the range at which to pursue
var chaseRange : float = 10.0;
// Print the points on-screen at the follower's last position (offset slightly)
var protectPoints : GameObject;
var cam : Camera;
var scorePosition = cam.WorldToViewportPoint (follower.position) + Vector2(-.025, .01);
var pointsClone : GUIText = Instantiate(protectPoints, scorePosition, Quaternion.identity);
// This is used to store the distance between the two objects.
private var range : float;
// This is the flag which indicates whether chasing has started
private var chasing : boolean = false;
private var scoreAmount = 100;
function Start(){
Score();
}
function Update(){
// Calculate the distance between the follower and the leader.
range = Vector3.Distance( follower.position,leader.position );
if ( !chasing range <= chaseRange ) {
chasing = true;
}
if ( chasing ){
// If the follower is close enough to the leader, then chase!
follower.LookAt(leader);
follower.Translate( speed * Vector3.forward * Time.deltaTime);
} // End if (range <= chaseRange)
} // End function Update()
function Score(){
while(true) {
if (chasing)
if (ProtectMode.protectMode){
ProtectMode.scoreProtect += scoreAmount;
FloatPoints();
}
yield WaitForSeconds (5.0);
}
}
function FloatPoints(){
pointsClone.text = scoreAmount.ToString();;
Destroy(pointsClone, 2);
}
I’m trying to work off some other posters’ code to achieve floating score text above some objects, but this error isn’t having any of it. As far as I can see, there are two object references in that line 18; cam and follower, and they’re both set. What’s happened??
Did you just select the objects from the dropdown menu or did you actually drag them into place from the hierachy menu? That might be it, broken or incomplete links. If it’s something else, BUMP!
Nope, they’ve all been dragged. Dragged hard. This is something else D=
Hello,
I don’t recall what the rules were on this, but it looks mighty questionable to have a variable assignment based on another variable. I would l suggest moving your assignment code to the Start() function, i.e:
var scorePosition;
function Start(){
scorePosition = cam.WorldToViewportPoint (follower.position) + Vector2(-.025, .01);
}
Good luck!
-Lincoln Green
Thanks man! Hmm, doing that gives me the same NRE for the next line:
var pointsClone : GUIText = Instantiate(protectPoints, scorePosition, Quaternion.identity);
This scorePosition variable looks dodgy. But I don’t understand enough to figure it out…
Hello,
That’s because you’re still assigning a variable based on another variable. You’ll need to move all your variable assignments that make use of other variables into the Start() function.
Regards,
-Lincoln Green
OK, that’s sensible. I moved that line assigning var pointsClone into Start() as well:
function Start(){
var scorePosition = cam.WorldToViewportPoint (follower.position) + Vector2(-.025, .01);
var pointsClone : GUIText = Instantiate(protectPoints, scorePosition, Quaternion.identity);
Score();
}
No more NRE, but that line within Start() now gives me “InvalidCastException: Cannot cast from source type to destination type”. The FloatPoints() function also gives “Unknown identifier: ‘pointsClone’”:
function FloatPoints(){
pointsClone.text = scoreAmount.ToString();;
Destroy(pointsClone, 2);
}
I can’t declare a function inside a function, so how can I get FloatPoints to find the reference again?
Try something like:-
var pointsObj: GameObject = Instantiate(protectPoints, scorePosition, Quaternion.identity);
var pointsClone : GUIText = pointsObj.GetComponent(GUIText);
Thanks andeeee. After much (mostly blind) tweaking, it now works without error.
A quick positioning question to tidy it up:
var scorePosition = cam.WorldToViewportPoint (follower.position) + Vector2(-.025, .01);
This line prints the score at the follower’s position onscreen. The text stays at that same position until it is destroyed. Is it possible to print the score at the follower’s position within the scene, rather than the onscreen coordinates? So, the 2D text appears and displays the same, but, instead of staying ‘stuck’ on the camera, it moves relative to it (just like any other object in the scene).