Using an Array of Components to Control an Object

I’ve been attempting to convert code from a Unity3d Car Tutorial into C# and it compiles but pretty much doesn’t work at all. I just get a null reference from the get go. Can anybody take a look at this and see what I’m missing or doing wrong.

By the way, the car tutorial is at:
http://www.gotow.net/andrew/blog/?page_id=78

I think it’s a very good tutorial but missing details that would help me put everything together and the fact that I’m a Unity3d newbie.

All I’m looking for my code to do is make my object track through waypoints. I don’t need all the other extras.

Here’s my code:

using UnityEngine;
using System.Collections;

public class RailCarScript: MonoBehaviour
{
GameObject waypointContainer;
Component[ ] s;
Vector3 RelativeWayPointPosition;
private ArrayList waypoints;
private int currentWaypoint;

// Use this for initialization
void Start ()
{
GetWaypoints();
}

// Update is called once per frame
void Update ()
{
//transform.Translate(25*Time.deltaTime, 0, 0);

if(RelativeWayPointPosition.magnitude < 15)
{
currentWaypoint++;
}

if(currentWaypoint >= s.Length)
{
currentWaypoint = 0;
}

NavigateToWaypoint();
}

void GetWaypoints ()
{
Component[ ] potentialWaypoint = waypointContainer.GetComponentsInChildren(typeof(Transform));

waypoints = new ArrayList();
Component[ ] s = (Component[ ]) waypoints.ToArray(typeof(Component));

foreach(Component a in potentialWaypoint)
{
if(a.transform != waypointContainer.transform)
{
s[s.Length] = a;
}
}
}

void NavigateToWaypoint ()
{
RelativeWayPointPosition = transform.InverseTransformPoint(s[currentWaypoint].transform.position.x, transform.position.y, s[currentWaypoint].transform.position.z);
}
}

Thanks.

Dudes, I know this is necroposting, but I also need this script in c#. Can someone help with it?

Hi,

It helps if you paste the actual error, but null references usually mean you forgot to drag and drop something in the Inspector. Check there first, then look through the code, but honestly this should be fairly easy to spot with the error message / traceback.

As a general rule, I highly recommend using the “this” keyword whenever using a class field/variable. e.g. instead of just typing waypointContainer, type this.waypointContainer. This does two things. first it makes things more readable and catches errors (if you type ‘this.’ you will know instantly if you are calling something that exists on the class, or you will see an error) and second, it eliminates ambiguous names. For example, I could have a ‘this.name’ AND a ‘name’ in the same function and it is perfectly legal. If you always use ‘this.’ having both names will not be confusing either.

Another general tip is I would never use single letter variables (unless they were standard. The most common ones are i for a number, usually an int used for counting, then x, y, z, u, v, s, r, t, sx, sy, sz, rz, ry, rz, tx, ty, tz - I’m sure there are a few more too)

Readability is king and writing nice code can be a debugging tactic all on its own. I can’t count the number of times I’ve found the source of a bug simply by cleaning up someone’s code.

This is all general stuff though. If you are having a problem, please post the error message.