- Home /
Fixing the NullReferenceException.
Hi guys.I'll get straight to the point. I have been able to fix this error in the past but this time I just can't seem to do it and form my search I have found nothing of use that could aid me in this.
I have created a script for moving a platform(Platform Movement) a certain distance when the player steps on it.The first problem that occured was that when I called the script in my Player script that was checking if a trigger was entered nothing happened at all.I then proceeded to call the MovePlatform()
function in the Update()
inside the Platform Movement script and call for the update inside the Player script in the OnTriggerEnter()
function which resulted in the platform moving to it's position as soon as the game was started.
To get around this I added a condition which had to be true in order for the platform to move inside the Update()
in the Platform Movement script.Even though the platform is not moving as soon as the game starts,when the player lands on the platform I recieve the NullReferenceException error :
NullReferenceException: Object reference not set to an instance of an object Player.OnTriggerEnter (UnityEngine.Collider object) (at Assets/Scripts/JavaScripts/Player.js:28)
This error only occurs as long as I have a condition in place for the platform and is occurring at the point where I'm calling the Update function.
Here are the code snipets that do the work. The Platform Movement script attached to the platform:
var moveSpeed : float;
var destinationPos : float = 20;
private var onPlatform = false;
function MovePlatform()
{
destinationPos = transform.position.z;
if(destinationPos < 20)
{
transform.position.z += moveSpeed * Time.deltaTime;
}
}
function Update()
{
//The if statement here prevent the platform from moving off at the start of the
//game and should allow the platform to move when the player is on it.
if(onPlatform == true)
{
MovePlatform();
}
}
And here is the part of the Player script that checks if the trigger with the specific tag has been entered:
static var script : PlatformMovement;
function OnTriggerEnter(object : Collider)
{
if(object.gameObject.tag == "Platform2")
{
script = GetComponent(PlatformMovement);
script.Update(); //The error is occurring here
}
}
I have tried attaching the game object that contains the Platform Movement script to the player but when the player lands on the platform he is parented to it and then the Platform Movement script is deleted from the inspector.
I am out of ideas and means to try to fix this and have no clue how to get the platform to move only when the player is on it.
Any help or hints would be greatly appreciated.
I think, don't use 'object' for your collider name. (I could be wrong)
Also try using the triggers or onCollisionEnter on the platform ins$$anonymous$$d of the player Edit: you did, ok I misread :P
I see, script.Update()
I don't think you can call update like that. It is automatically executed once per frame, per script anyway.
On the platform script, reference the player object. In the collider function use this new reference to update the players transform.
Any ideas what i should call it then?
I tried using triggers on the platform but I got no feed back at all from it.And since Debug.Log()
worked on the player I went with it.Also the level has more than one platform so I thought that if i checked when the player entered a specific trigger with the tag it would allow me to move only the platform that the player was on.
As in if I have the platform checking if the player has entered the trigger the my player has the tag player.And since all the platforms would have the same Platform movement script attached I thought that if I entered platform 1 then platform 2 would move also.But by checking it this way only the platform that the player is on should move.
col :) seems to be what most use. I'm not sure but object is a keyword like static so it'll throw out problems. You can call your collider anything you want as long as its not a taken name.
Calling Update()
probably is yes :) , but calling something like Start()
manually is sometimes helpful.
A small notice, you can use CompareTag ins$$anonymous$$d in OnTriggerEnter, it's faster. Otherwise tag is also a derived member of a Collider, so you don't need the overhead of gameObject.
Answer by gamerant · Aug 20, 2013 at 11:43 PM
Thank you for all the suggestions here but I figured it out in the end :) @Perter G,No the script wasn't meant to be destroyed but I think it was simply because the player was being parented to the platform at run time.
For those interested this is how i fixed it.I first went back to my PlatformMovement
script and changed it so that instead of the player becoming the child of the whole platform that included it's children, it would become the child of the platform that moved when it landed on it.Also I am not calling or referencing any scripts here. Here is the altered script for the PlatformMovement
:
var moveSpeed : float;
private var destinationPos : float;
var targetPos : float;
public var parentObject2 : Transform;
public var childObject2 : Transform;
private var onPlatform = false;
function MovePlatform()
{
//Makes the platform move in the negative z direction.
//If you want positive just change the minus sign to a plus.
destinationPos = -transform.position.z;
if(destinationPos < targetPos)
{
transform.position.z -= moveSpeed * Time.deltaTime;
}else if(targetPos == targetPos)
{
onPlatform = false;
}
}
function OnTriggerEnter(object : Collider)
{
if(object.gameObject.tag == "Player")
{ onPlatform = true;
//Makes the player a child of the moving platform upon,
//entering the trigger.
childObject2.transform.parent = parentObject2.transform;
Update();
Debug.Log("I'm here");
}
}
function OnTriggerStay(object : Collider)
{
if(object.gameObject.tag == "Player")
{
childObject2.transform.parent = parentObject2.transform;
}
}
function Update()
{
if(onPlatform == true)
{
MovePlatform();
}
}
Note: You can remove one of the parenting statements and it should still work.
The only strange thing that seemed to occur is that you need to add a collider to your game object and make it a trigger.Then create an empty game object and call it "collider" and make it the child of the platform.Then add a box collider to this child object and make it the size of your game object. P.S: I tried the other way,where the trigger is the child of the game object but that didn't seem to work.
Hope this helps anyone else and again.Thank you for all the suggestions and hints as well as tips :)
Answer by Amon · Aug 14, 2013 at 12:13 PM
I use to get this. It had to do with script execution order. Basically one script needed to be compiled first in order for the 2nd to be able to work and not throw this kind of error.
It may be worth having a look at that.
Thanks for that.I tried changing the order in which the scripts are done and I either end up having the player on the platform and the platform is motionless or I get the exact same error(depending on the order) :D
Answer by Peter G · Aug 14, 2013 at 01:52 PM
Try this:
var script : PlatformMovement;
//This shouldn't be a static variable.
function OnTriggerEnter(object : Collider)
{
if(object.CompareTag("Platform 2")
{
script = other.GetComponent(PlatformMovement);
//The line below is throwing the error, but the problem is with this line.
// You aren't accessing the component.
//If this is attached to the player, thenyou want to get the component
//on the collider
script.Update(); //The error is occurring here
}
}
I tried this what you said @Peter G and what you suggested too @meat5000 and in the end I get the error: Object reference not set to an instance of the object.
I changed the script.Update()
to script.$$anonymous$$ovePlatform()
. I've been looking at how things happen during play in the hierarchy too and noticed that when the player hits the platform he is parented to it which is what i want.But the script that I'm referencing gets deleted from the inspector within the player once the Player is the child of the platform and that is when I get the error.
So I've been thinking if there is some way of making the script part of the player object during run time when the Player becomes the child?$$anonymous$$aybe that could solve it.
I would say that practically, you could make that work. Conceptually though, that's just not a good practice. Now you have the script that controls the platform attached to the player. It would confuse anyone looking at it.
On other notes, you probably should rename object
. It's a reserved word and even if the compiler let's you use it, it will also confuse someone looking at your code.
And why is your Platform$$anonymous$$ovement
script getting deleted. I'd try and figure that out first. Is is supposed to be destroyed?