Noob programing problem (C#)

Hi ! this is my simple class Waypoint :

public class WayPoint: MonoBehaviour {
Transform[ ] waypoint;
int currentWayPoint;

  • void Start ()*
    {
    waypoint[0] = GameObject.Find(“WP0”).transform; // Here
    waypoint[1] = GameObject.Find(“WP1”).transform; // herre
    waypoint[2] = GameObject.Find(“WP2”).transform; // and Here
  • }*
    }

when i "play " the game, i have a alert message : “Assets/Scripts/WayPoint.cs(5,17): warning CS0649: Field WayPoint.waypoint' is never assigned to, and will always have its default value null’” (it is the Transform[ ] waypoint; line)

when i create a new instance of my class, i have a error message “Array index is out of range.” (for the 3 “//here” lines)

What is the problem ? i suppose it is a stupid error i did, but where ? (i dont want a fixed size (3) Array, i juste have 3 waypoint here, but it will be random later)

Your array is null. In the Start try getting all the waypoints (use a tag for them and find objects by tag) and use that array (array of gameobjects) or use that count to instantiate your array of transform, then loop the found objects to assign the transforms :wink:

Or try to use List instead of an array ?

List waypoint = new List();
int currentWayPoint;

void Start ()
{
GameObject goWaypoint0 = GameObject.Find(“WP0”);
if(goWaypoint != null)
waypoint.Add(goWaypoint0.transform);
[…]
}

ok, i’ll try this, thx

i’v try this, but Visual Studio said “The type or namespace List could not be found”.

thx to you :slight_smile:

List is in System.Collections.Generic

[B]Transform[] waypoint = new Transform[3];[/B]
void Start ()
{
waypoint[0] = GameObject.Find("WP0").transform; // Here
waypoint[1] = GameObject.Find("WP1").transform; // herre
waypoint[2] = GameObject.Find("WP2").transform; // and Here
}

Why not make the waypoint array public and just drag the objects onto it in the editor?

You’d be able to stop a null index fairly easily that way and it’d be easy to reuse later.

List requires you to have: using System.Collections.Generic;