- Home /
Moving a character to a waypoint
I have a character named damsel that spawns at a random point and then is supposed to move to a way point at the centre of the screen. The problem is the damsel wont move after spawning and i get error:
NullReferenceException: Object reference not set to an instance of an object
Iv had this error plenty of time before but this time I'm just not sure what to do about it, here is the code:
public class Damsell : MonoBehaviour { public float moveSpeed = 1f; public float health = 1f; private Transform target; public Transform centreWaypoint;
void Start()
{
centreWaypoint = CentreWaypoint.centrepoint[0];
}
void Update ()
{
Vector3 dir = centreWaypoint.position - transform.position; //this is the line with the error
transform.Translate(dir.normalized * moveSpeed * Time.deltaTime, Space.World);
Ill keep working on it, I'm sure the answer is staring at me but i am quite new to this.
Answer by bolkay · May 16, 2018 at 04:35 PM
//try
centrewaypoint.transform.position
Then check your inspector to see if you've properly assigned stuffs.
The strange this is that i cannot put the centreWaypoint in the public slot in the inspector, this might be because the damsel is a prefab and the waypoint is an empty object in the scene.
Where would you put that line, it just gives me the same error
I think i need to reference the centreWapoint somehow but it wont seem to let me.
Well its sort of working now, i'm not getting the error anymore and the damsel is moving to where I tell it to even though i don't feel like i have done it correctly.