array using getcomponents (c#)

Hello am struggling with the ol’ js AIcar_script :°)
Thanks to Guyt for converting it in c# in another post…
BUT…
I still can’t get it work in C#, when the js is doing the routine…
I get “Obj ref not set to an instance” pointing at this line :

Vector3 RelativeWaypointPosition = transform.InverseTransformPoint( new Vector3(waypoints[currentWaypoint].transform.position.x, transform.position.y, waypoints[currentWaypoint].transform.position.z ) );

I tried other ways, but it seems the c# script can’t retrieve the potentialWaypoints from the GameObject. :°(
I hope someone can help since it’s the only example of my knowledge using “getcomponentsinchildren” to populate the array, rather than instantiating the waypoints manually.

Below the condensed version of the waypoint system :
Thank for your help!

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    
    public class AICar : MonoBehaviour {
    
    	public GameObject waypointContainer;
    	private List <Transform> waypoints;
    	public int currentWaypoint = 0;
    
    
    	void Awake () {
    	GetWaypoints();
    	}
    
    
    	void GetWaypoints ()
    	{
    		if (waypointContainer!=null)
    		{
    			Transform[] potentialWaypoints = waypointContainer.GetComponentsInChildren <Transform> ();
    			waypoints = new List<Transform>();
    			foreach (Transform potentialWaypoint in potentialWaypoints) 
    			{
    				if (potentialWaypoint != waypointContainer.transform)
    					waypoints.Add(potentialWaypoint);
    			}
    		}
    	}	
    
    
    	void Cruise () {
    		Vector3 RelativeWaypointPosition = transform.InverseTransformPoint( new Vector3(waypoints[currentWaypoint].transform.position.x, transform.position.y, waypoints[currentWaypoint].transform.position.z ) );
    		Vector3 RelativeWaypointDir = ((waypoints[currentWaypoint].transform.position) - transform.position);
    		float currentAngle = Vector3.Angle(RelativeWaypointDir, transform.forward);

// then IA torque, steering, or animated NPC can use the previous these values

    		
    		if ( RelativeWaypointPosition.magnitude < 15 ) 
    		{
    			currentWaypoint ++;
    			
    			if ( currentWaypoint >= waypoints.Count )
    				currentWaypoint = 0;
    		}
    	}
    }

Just analyse the line. Null reference exceptions happens when you try to use a class instance but the variable doesn't hold a reference to a valid object.

Vector3 RelativeWaypointPosition =                        // 1
    transform.InverseTransformPoint(new Vector3(          // 2
        waypoints[currentWaypoint].transform.position.x,  // 3
        transform.position.y,                             // 4
        waypoints[currentWaypoint].transform.position.z   // 5
    ));

  • 1: should be no problem, creating a variable that way can't cause a null ref exception.
  • 2,4: As long as your script is a Monobehaviour-component it have to be attached to a GameObject and therefore there have to be a transform component. The inverse transform and the new Vector3 also should be no problem since we can be sure the transform is there.
  • 3,5: that two "parts" look like it can cause that error. I guess the currentWaypoint variable is in bounds of the array (which means greater or equal to 0 and less than array.Length) otherwise you would get an out-of-bounds-exception. It looks like the only thing left over are the objects stored in the array.

Make sure that every element in the array is filled with a valid GameObject. If you set the array-size to 10 but you only drop 3 waypoints into the array you will have 7 null-references which would cause that error when you try to access it's transform.

Reduce the array size to the actual size or fill in the other waypoints.

Back in the topic, I could finally make it work :°D

  • I changed “void Start” into “void Awake”, after I run into this :

“Note for C# and Boo users: use Awake instead of the constructor for initialization, as the serialized state of the component is undefined at construction time.”

  • and I added a “waypoints.count” to be sure to keep within the array bounds

      	foreach (Transform potentialWaypoint in potentialWaypoints) 
      	{
      		if (potentialWaypoint.transform != waypointContainer.transform)
      			for (int i=0;i<waypoints.Count;i++);
      			waypoints.Add (potentialWaypoint);	
      			
      	}
    

So this waypoints system is working fine!

Make sure currentWaypoint is in bounds