- Home /
object reference not set to an instance of an object
var speed : float;
var turn : float;
function Update()
{
var targets : GameObject[] = GameObject.FindGameObjectsWithTag("target");
var closest : GameObject;
var closestDist = Mathf.Infinity;
for (Target in targets)
{
var dist = (transform.position - Target.transform.position).sqrMagnitude;
if(dist < closestDist)
{
closestDist = dist;
closest = Target;
}
}
var newRotation = Quaternion.LookRotation(
closest.transform.position - closest.position, Vector3.forward);
transform.rotation = Quaternion.Slerp(
transform.rotation, newRotation, Time.deltaTime * 8);
transform.position += transform.forward*speed*Time.deltaTime;
}
var speed : float; var explosion : Transform;
// Find the name of the closest enemy function FindClosestEnemy () : GameObject { // Find all game objects with tag Enemy var gos : GameObject[]; gos = GameObject.FindGameObjectsWithTag("Respawn"); var closest : GameObject; var distance = $$anonymous$$athf.Infinity; var position = transform.position; // Iterate through them and find the closest one for (var go : GameObject in gos) { var diff = (go.transform.position - position); var curDistance = diff.sqr$$anonymous$$agnitude; if (curDistance < distance) { closest = go; distance = curDistance; }}
var newRotation = Quaternion.LookRotation(closest.position - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 8); transform.position+=transform.forward*speed*Time.deltaTime; }
function OnCollisionEnter(collision : Collision){ Instantiate (explosion, transform.position, transform.rotation); Destroy(gameObject);}
missile not moving
use code block to format otherwise nobody gonna read this !
@umair_re : Dude, put some time and thought into asking your question. If you do so, you will get good answers. And for sure, format your code using 'code block' like @liszto mentioned.
It'll be nice if you could format and edit your original question ins$$anonymous$$d of reposting a similar thing as a comment as well.
Read FAQ on guidelines for writing good questions?
Which line of your code has the error on it? If you double click on the error in the Unity console it should jump to the line with the error. We need to know which line that is.
NullReferenceException: object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.InvokeBinaryOperator(System.String operatorName, System.Object lhs, System.Objecy rhs)
Answer by Benproductions1 · Feb 14, 2013 at 10:54 PM
Your not checking wheather or not closest
exists. If it doesn't find any objects, closest
will be null, therefore the null reference exception.
Thats at least one of the problems...
Hope this helps, Benproductions1
Your answer
Follow this Question
Related Questions
transforming position to the location of a variable 1 Answer
Transform position over X amount of time , how ?? c# 2 Answers
bullet's flight adjusts position according to a parent object ?? 1 Answer
Transform.LookAt 2 Answers
NullReferenceException 2 Answers