- Home /
Vector3.Distance error plizzzzzz help
im always getting a NullReferenceException: Object reference not set to an instance of an object at line 4 no matter what i try, there is a game object with tag Enemy but it wount work
function Update () {
var target = GameObject.FindWithTag ("Enemy");
if ( Vector3.Distance(target.position, transform.position ) < 25) {
print("found it");
}
}
Comment
Best Answer
Answer by Sequence · Feb 18, 2012 at 07:24 PM
If you're looking for a gameobject with the tag player, then trying to get the position from it like this:
Player.position
That won't work, because to get the position of a gameobject, you must first access the gameObject's transform.
Player.transform.position
The same is with rotations too:
Player.transform.rotation
Link to GameObject documentation: Click Here
Answer by Kona · Feb 18, 2012 at 06:56 PM
the first line after you open up the function should be GameObject.FindGameObjectWithTag( "Enemy" );