- Home /
Prefab movement towards target
How to move my prefab towards a target?
I have a missile as a prefab and i want it to move towards enemy ship which is the target.
I have written a script to move the prefab public Transform target Void Start() { target = GameObject.find("Enemy").transform; }
void Update () { transform.position = Vector3.MoveTowards(transform.position, target.transform.position, 1.0f); }
but when i play it says NullReferenceException: Object reference not set to an instance of an object
public Transform target Void Start() { target = GameObject.find("Enemy").transform; }
void Update () { transform.position = Vector3.$$anonymous$$oveTowards(transform.position, target.transform.position, 1.0f); }
Answer by JoshDangIt · Oct 28, 2015 at 05:01 PM
The method name is "GameObject.Find", not "GameObject.find". Methods are case sensitive.
if that isn't the issue, make sure the name of the gameobject is "Enemy". These names are case sensitive as well.
Your answer
