- Home /
Array of custom class objects all return the same value?
I'm having trouble with a system of creating waypoints and the way that my custom waypoint class is being referenced. My code is as follows:
in a file called Waypoints attached to my Player
public var location: Vector3; public var attitude: Quaternion; public var pathType:int;
function SetWayPoint(tr_Loc:Vector3,tr_Rot:Quaternion,pT:int){ location = tr_Loc; attitude = tr_Rot; pathType = pT; }
in a file called PathCreate attached to my Player
var wayPts: Waypoints[] = new Waypoints[21];
function FirstMove() { wayPts[0].SetWayPoint (transform.position,transform.rotation,1); wayPts[1].SetWayPoint (transform.position,transform.rotation,2); wayPts[2].SetWayPoint (transform.position,transform.rotation,3);
print(wayPts[0].pathType+" "+wayPts[0].pathType+" "+wayPts[0].pathType); }
If I run the program and call "FirstMove()", I get a "NullReferenceException: Object reference not set to an instance of an object"
If I go to the Player in the Inspector and select the PathCreate script, then select the elements of "Way Pts" and change them from "None (Waypoints)" to "Player (Waypoints)" [based on the pull-down], the program runs with no error, but my printed output is "3 3 3" instead of the expected "1 2 3". I'm guessing this means that all of the waypoints in the array are writing to the single waypoint data from the script, but I don't know how to prevent this. Any help would be appreciated! Thanks!
Sorry, I edited the above to add the "pathType" component of Waypoints so that my problem would be more clear.
Answer by Tetrad · Jun 09, 2010 at 08:03 PM
Initializing the array of WayPoints doesn't initialize each instance of Waypoints. So after new Waypoints[21] is called, you have an array of size 21 that points to 21 null Waypoints objects.
Now if Waypoints has to be a MonoBehaviour you're kind of out of luck unless you hook things up in the editor, but if you just want your Waypoints class to be a thin wrapper for position and rotation data you could just make it a c# struct so you don't have to do anything (all value types get initialized to zero). Or you could make it a c# class, but you'd have to new off each instance of it like this:
wayPts[0] = new WayPoints();
wayPts[0].SetWayPoint( /*whatever*/ );
I don't know how you'd go about doing that in Javascript, though, since all classes are assumed to derive from MonoBehaviour.
Edit: If you want to add a bunch of waypoint objects, do this:
wayPts[0] = AddComponent( "WayPoints" );
wayPts[0].SetWayPoint( /*whatever*/ );
Thanks! I'm going to try and stick with Java for the moment. I'll see if anyone knows how to hook up each instance in JavaScript, or, more importantly, how to get each instance to register when I attempt to fill it via its index.
Actually here's what you need to do:
wayPts[0] = AddComponent( "WayPoints" ); wayPts[0].SetWayPoint( whatever );
I tried the AddComponent, but I got an 'AddComponent' is not a member of 'Waypoints' error. I haven't extended Waypoints from anything (unless it is the automatic extension from $$anonymous$$onoBehavior), so maybe this is why...
Are you sure you used it correctly? You shouldn't be doing wayPts[0].AddComponent. I edited my original post.
Also by default, your classes made in javascript all extend from $$anonymous$$onoBehaviour.
I did it exactly like you posted: wayPts[0] = AddComponent("Waypoints"); wayPts[0].SetWayPoint (...
I also tried it the other way, and I tried it with a GameObject.AddComponent as well. All of them choke on the "AddComponent"...
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Instantiate into array : Out of range? 1 Answer
Can i use objects from a builtin Array to populate a Js Array 2 Answers