- Home /
Trying to make an object "Flee" from the player but it won't work?
I tried this code out and it doesn't work, an error pops up saying "Object reference is not set to an instance of an object (target)" my code (C#):
using UnityEngine;
using System.Collections;
public class Flee : MonoBehaviour {
public int moveSpeed = 10;
new Vector3 dir;
GameObject target;
void Start(){
target = GameObject.FindGameObjectWithTag("Player");
}
void Update(){
dir = transform.position - target.transform.position;
transform.Translate(dir * moveSpeed * Time.deltaTime);
}
}
you don't need a new on line 6:
new Vector3 dir; ? -> Vector3 dir;
EDIT: Oh so I just learnt something, what I wrote is not your problem, likely getyour411 is correct.
new Vector3 dir; simply overwrites any variable called dir in the base class, which in this case doesn't exist, and so makes no difference, though I guess in that case convention would still suggest not writing new!
To help answer: Try adding a Debug.Log after you set target -
void Start(){
target = GameObject.FindGameObjectWithTag("Player");
Debug.Log(target);
}
it will likely return null!
$$anonymous$$y player is indeed tagged as Player so.....I really am not sure
Option #2: The error is not co$$anonymous$$g from this code; as you have it posted a simple copy/paste into my project works fine (albeit the flee is more like a take-off into space) but nonetheless it works without error.
Answer by Suppapa · Sep 07, 2015 at 03:47 AM
All I have to do is make the objects be prefabs and then instantiated for some reason
Answer by wibble82 · Sep 06, 2015 at 10:25 AM
Hi
It's a bit hard to diagnose from just that code, but if it was me the first thing I would try is moving your 'FindGameObjectWithTag' line to inside the update function, and then checking it is valid each frame
void Update()
{
target = GameObject.FindGameObjectWithTag("Player");
Debug.Log(target);
if(target != null)
{
dir = transform.position - target.transform.position;
transform.Translate(dir * moveSpeed * Time.deltaTime);
}
}
This is definitely not the best way to do things, as that search will be slow. However I suggest it to validate the fact that you are finding the correct game object. If you are indeed detecting it correctly in start, that would suggest the object you found was being destroyed. If so, you'll need to work out why, and either make it find the correct object, notify the script when the object changes, or just do the search every frame the dodgy way!
-Chris
Answer by ywj7931 · Sep 06, 2015 at 10:34 AM
are you sure you have set the tag?
try to use GameObject.FindWithTag im not sure about findgameobjectwithtag
Your answer
Follow this Question
Related Questions
When I try to save the velocity of a rigidbody and use it later it doesn't work? 1 Answer
Rotation drift with transform.eulerAngles 0 Answers
My objects start flying after I Instantiate them 0 Answers
How can I slow the velocity of my player without it affecting fall speed? 0 Answers
Parent mesh problem 0 Answers