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 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.

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
    }
}

Thank you for all the suggestions here but I figured it out in the end :slight_smile: @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 :slight_smile: