- Home /
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;
}
}
}
This script is new for me, but as far as I know you must create an empty object (the container) and child all waypoint objects to it, then assign the container to waypointContainer. Just to check: have you did this?
Answer by Bunny83 · Sep 09, 2011 at 01:33 AM
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.
I've actually didn't read the whole script. I was thinking your waypoints variable is an array but it's a generic List.
Are you sure that you assigned your waypoint container to the variable? If not the whole waypoints List doesn't get created at all.
To spot such errors just place some temporary Debug.Logs in those functions to see what gets executed and what didn't.
Btw: You store Transforms in the List so there's no need to write
waypoints[currentWaypoint].transform.position.x
since waypoints contains Transforms just write
waypoints[currentWaypoint].position.x
otherwise you tell Unity to search for the first Transform component on the Gameobject of this Transform (which IS that Transform). It's like writing something like:
transform.transform.gameObject.transform.position.x
Thank you all for your comments... Well, as said first, I run the "same" script but in js aside, with +400 waypoints in the waypointContainer... all working fine in the case of the javascript array. C# arrays are a bit more complex apparently...
I left out the code lines that drive the NPC, mostly pointing at another script (reason why I would like to have all in c#)... As a transform component, I use myTransform = transform, what I left out as well. So I pasted here only the waypoints system.
BTW I can't really get what Transform is concerned by the first "transform.InverseTransformPoint" ? The script transform or the Waypoint one? But anyway, I get the same Null ref with the following RelativeWaypointDir
:(
Yes Bunny, I just noticed the redundance as well... I fixed it but the Null ref persists. The use of the generic list is motivated by the need to have a flexible array at edit time.
Still stuck... I tried to turn the generic list into an arraylist, but do you know why the components are not accessible then? It returns "Type object' does not contain a definition for
Transform' ???
Answer by thieum · Sep 11, 2011 at 03:34 AM
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!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Assign a String to Each int 1 Answer
List out all Array Values. 1 Answer
How to Clear Array or Remove it's items 3 Answers