Can't find game object

So I want to set the variable “theplayer” to the position of the player. I find the gameobject and print it to the console. The console says it found it. However when I use the variable for my raycast, I get an
error : “NullReferenceException: Object reference not set to an instance of an object”. Here is the code for finding the object.

theplayer = GameObject.Find("FPSController");
Debug.Log(theplayer);

Here is the raycast part,

var hit : RaycastHit;

if (Physics.Raycast(transform.position,theplayer.position)) {
    Debug.DrawLine (transform.position, hit.point, Color.green);
}

This is all in the update function.
Thanks for the help.

I don’t know how it works in Javascript or I’d give you the code, but basically you create a “hit” object and never set it to anything. Your Physics.Raycast is finding a hit, but not storing it in your “hit” variable. You either have to pass “hit” into the Raycast function or set “hit” equal to the return value of the Raycast function.

Hope this helps!

Edit: You’ve sent two positions into Physics.Raycast when it expects a position and a direction. The code will work, but it’s not going to do what you want it to do.

Thanks for the reply.

For some more context, I have two positions in the raycast because it is a line of site. So the raycast goes between the two objects.

Sorry, I don’t know what you mean by storing the raycast hit into the hit variable. What do I put in it? theplayer variable?

Thanks for the help!

As Gambit pointed out, there is a mistake in how you use RayCast. You need to send a position, direction, and can optionally send a max range. You can calculate a vector (direction + magnitude) by taking the difference between two points. To turn a vector into just direction, you can normalize it. (Technically a normalized vector still has a magnitude of 1.) However, I don’t think RayCast is so picky that we need to do that…

thePlayer = GameObject.Find("FPSController");
myPosition = transform.position;
directionToPlayer = thePlayer.transform.position - myPosition;
sightDistance = 10f;
var hit : RaycastHit;
Physics.Raycast(myPosition, directionToPlayer, sightDistance);

Now about that null reference… It looks like GameObject.transform might return null. So you should check that there is a transform on your GameObject(s); both on thePlayer and this.

You don’t want to send the player’s position in as the direction of the raycast. Instead, you want a vector from your object to the player. To get a vector between two vectors, you subtract like this:

var vecBetween = endVector - startVector;

My previous post was incorrect (sorry!) Physics.Raycast does not return a RaycastHit object, but outputs it through a parameter. You can look up output parameters if you want, but basically you send in your “hit” variable and the function fills it out for you.

Try replacing your code with this:

var hit : RaycastHit;

var vecBetween = thePlayer.position - transform.position;

if (Physics.Raycast(transform.position, vecBetween.normalized, hit, vecBetween.magnitude))
{
    // Physics.Raycast returned true, so "hit" has the info we need
    // if Physics.Raycast returned false, "hit" would be null
    Debug.DrawLine (transform.position, hit.point, Color.green);
}