I wouldn't say i'm new to unity or programming in general.. but right now i feel so stupid.
I have a simple enemy script. I instantiate object with another script, calling SettingUpCreep and giving it a list of waypoints. Then happens update object should move from one waypoint to another. But look at information in Console.
What's wrong with waypoints.Count in Update? Why does it increased? And it works only if list and nextPos are public. If i set it to private then other methods can't access it. There's nothing in it.
What's happening and why? I can't understand. Have i've done something wrong or just need more sleep?
List<Vector3> waypoints;
Vector3 nextPos;
int currentPoint = 1;
private void Update()
{
moveToNextWaypoint();
}
void moveToNextWaypoint()
{
Debug.Log("Update");
Debug.Log("WP.count " + waypoints.Count + " NP" + nextPos);
if (transform.position == nextPos)
{
if (currentPoint == waypoints.Count)
{
gameObject.SetActive(false);
}
else
{
currentPoint++;
nextPos = waypoints[currentPoint];
}
}
transform.position = Vector3.MoveTowards(transform.position, nextPos, 5 * Time.deltaTime);
}
public void SettingUpCreep(List<Vector3> list)
{
Debug.Log("list.Count " + list.Count);
List<Vector3> waypoints = new List<Vector3>(list.Count);
for (int i = 0; i < list.Count; i++)
{
waypoints.Add(list[i]);
}
nextPos = waypoints[1];
transform.position = waypoints[0];
Debug.Log("WP.count " + waypoints.Count + " NP" + nextPos);
}