Problem with converting JS to C#

Hi. I’m trying to convert the AI.js script from the Unity FPS tutorial to the C# language.

My first trouble was accessing the AutoWayPoint type in C#, as the script was in JS. I resolved that by converting that script also to C#.

When I start running the scene, there is a null reference exception in the new AutoWayPoint.cs script.

The code is the following:

using UnityEngine;
using System.Collections;

public class AutoWayPoint : MonoBehaviour {


	static ArrayList waypoints;
	public ArrayList connected;
	static float kLineOfSightCapsuleRadius = 0.25f;
	
	public static AutoWayPoint FindClosest (Vector3 pos) {
		// The closer two vectors, the larger the dot product will be.
		AutoWayPoint closest = null;
		float closestDistance = 100000.0f;
		foreach (AutoWayPoint cur in waypoints) {
			float distance = Vector3.Distance(cur.transform.position, pos);
			if (distance < closestDistance)
			{
				closestDistance = distance;
				closest = cur;
			}
		}

		return closest;
	}
...
...

The problem is in line 15:

foreach (AutoWayPoint cur in waypoints) {

The exception: NullReferenceException: Object reference not set to an instance of an object

Don’t know why it does that, so I need some help.

I never see where you store your waypoints basically its looking for them somewhere and they are not there.

Or you are trying to access it before there are any.

Well, the same code in JS runs perfectly. I just changed what was needed to convert to C# (syntax mostly).

The NPC runs perfectly with the original code but if I use my code, it just stands still in mid air and do nothing.

JS does a lot of automagic behind the scene if you don’t work with pragma strict. This includes autocasting, autocasting to a usefull type and access a field and all this kind of stuff that can make it pretty hairy to convert things.

For example if you have game objects in the array there and try to access autowaypoints in the loop, JS will likely work but on C# it will fail misserably because a game object is and never can be an autowaypoint, it can only contain a component of that type.

Add this line of code before the foreach loop:

AutoWayPoint closest = null;

That will create the AutoWayPoint instance with the value of null and you will avoid the “Null Reference Exception”.

Cheers!

Uhhh… I’m going to have to go ahead and disagree with you.

No problem. Have a different reason and solution? Go ahead and share.

How can you have an instance of something that is null? They are mutually exclusive concepts.

Making the value null will create the null reference exceptions, not avoid them.

I think the real cause to this is that you cannot run a foreach loop in C# against a null reference. In JavaScript, I believe you can (it will skip the loop) or it will fail silently and move onto the next line (I don’t think that happens in Unity). So the solution would be to make sure that an ArrayList has been instantiated and assigned to the “waypoints” field.

Seeing as this thread is almost a year old though, I don’t think it matters anymore to revive or solve the issue.

Use:
http://www.m2h.nl/files/js_to_c.php