Question by
RainKiller27 · Jul 28, 2016 at 12:20 PM ·
c#
Object reference not set to an instance of an object
Hello I need help for this piece of code for a mate can anyone help for line 17 it's spamming the same error over and over again in console
using UnityEngine;
public class Enemy : MonoBehaviour{
public float speed = 10f;
private Transform target;
private int wavepointIndex = 0;
void start()
{
target = Waypoints.points[0];
}
void Update()
{
Vector3 dir = target.position - transform.position;
transform.Translate(dir.normalized * speed * Time.deltaTime, Space.World);
if (Vector3.Distance(transform.position, target.position) <= 0.2f)
{
GetNextWaypoint();
}
}
void GetNextWaypoint()
{
if (wavepointIndex >= Waypoints.points.Length - 1)
{
Destroy(gameObject);
return;
}
wavepointIndex++;
target = Waypoints.points[wavepointIndex];
}
}
Comment
Answer by Firelights · Jul 28, 2016 at 07:21 PM
Line 17 is empty, please update the code or say what the correct line is. ( Sorry that this is a awnser but I can't comment).
Edit: If the problem is on line 15, its because Waypoints.points[0] is a prefab, there are 2 solutions:
**1.**Change it to a scene object. **2.**Instantiate a copy of that prefab to the scene. Like this:
target=Instantiate(Waypoints.points[0]);
Instead of:
target=Waypoints.points[0];
Rename your "start" method to "Start", case matters and that sets your target variable.
Your answer
