- Home /
Fixing this Null Reference Exception?
I'm trying to write a bit of code that send coordinates of one object to another script, but I'm getting a NullReferenceException.
The first script, Movement Point, has this:
var move : MovementScript = GetComponent(MovementScript); //x and y are established as the coordinates move.getMovementPosition(x, z);
The second script, MovementScript, has this:
function getMovementPosition(x : float, z : float) { //moves the player }
Both scripts have been attached to appropriate objects. Could anyone please offer a possible explanation for what is going wrong here?
The would have to be attached to the same object for GetComponent($$anonymous$$ovementScript) to find it... It seems you are implying that they are not...
Can you point me in the direction of how to access a different object's script?
Answer by whebert · Mar 17, 2013 at 11:23 PM
You can search for an object of that type and, if found, call methods on it.
var move : MovementScript = FindObjectOfType(MovementScript);
if(move)
{
move.getMovementPosition(x,y);
}
If you have more than one MovementScript object in your scene, you can use:
var movers : MovementScript[] = FindObjectsOfType(MovementScript);
Your answer

Follow this Question
Related Questions
Move Character By Left Clicking Mouse 1 Answer
Click to Move rotation + collider issue 0 Answers
Making a bubble level (not a game but work tool) 1 Answer
Help with turn based Movement in a grid 2 Answers
Mouse-click movement 0 Answers