- Home /
Not a member of Object..Can't get class.name from array
Hello people,
I am quite the newbie and I am completely stuck on a problem. I did quite some googling for an answer, but didn't succeed in finding the answer. This is my 3rd day I am trying to solve this.
I am trying to make a very basic game. I made a mission generator script who generates an assignment for the player to fly to in a chopper.
I am placing empty gameobjects all over the world with a script on it which defines it's name (nameCountry) and area of influence (range). The variables are being pushed in to an array.
#pragma strict
var range:float;
var nameCountry:String;
function Start () {
var myNode: CountryNode = new CountryNode(transform.position,range,name);
GameObject.Find("Main Camera").GetComponent(CountryDistanceCheck).countryNodes.push(myNode);
}
In a second script I want to test if the chopper is within the range of one of those GameObjects, so I am trying to match the name of all the objects in the array with the name of the assigment.
#pragma strict
var countryNodes= new Array();
var target: Transform;
var loopHandle :boolean = true;
var missionDestination:String;
class CountryNode {
var position:Vector3;
var range: float;
var nameCountry: String;
function CountryNode ( position:Vector3, range:float,name:String ){
this.position = position;
this.range =range;
this.nameCountry = name;
}
}
function Update () {
target = GameObject.Find("Helicopter").transform;
missionDestination = GameObject.Find("MissionMaker").GetComponent(MissionGeneratorV2_).destinationX;
//Debug.Log(missionDestination);
for ( var i =0; i < countryNodes.length; i++){
//var Country : GameObject= countryNodes[i];
Debug.Log(countryNodes[i]);
//Debug.Log(CountryNode(i).nameCountry);
Debug.Log(countryNodes.length);
}
}
I am iterating through the array of the empty GameObjects. But it keeps on saying that countryNodes[i].nameCountry doens't exist. While my debug checks do confirm I pushed the objects into the array.
Can somebody help me how to check the names of my objects in this array. It keeps saying nameCountry is not a member of object. Why o why?
It would be really a lot better if you forget about using Array. Use a generic List ins$$anonymous$$d, it's faster/better in every way and you won't have casting problems.
100% agree. Using a data structure that loses type is silly.
Note: in my second example (using the generic list) you'd have to add
//js
import System.Collections.Generic;
//cs
using System.Collections.Generic;
Answer by dubbreak · Feb 14, 2013 at 04:55 PM
Since you aren't getting an indexing exception it means the objects are in there. Problem appears to be that your array you are pushing them into isn't typed (should be an array of CountryNodes). I don't do JS, and some quick looking doesn't show a way to type the "array" (it's actually a list.. since it auto resizes etc).
The problem is: when you get your object out it is just an object, so at runtime it doesn't know whether it has your fields or not.
Solution? Cast you object before accessing the fields. A nice way would be to use a foreach loop that casts as you go.
foreach (var countryNode : CountryNode in countryNodes)
{
Debug.Log(countryNode.nameCountry);
}
//or using existing loop
for ( var i =0; i < countryNodes.length; i++)
{
var countryNode : CountryNode = countryNodes[i]; //helps type inference along
Debug.Log(countryNode.nameCountry);
}
Other solution: Use a typed structure.
var countryCodes : List.<CountryNode> = new List.<CountryNode>();
//in code that adds items
countryCodes.Add(myNode); //adds to end - push not avail in list, need to use stack for that
These type inference issues are one of the reasons I'm not a huge fan of unity's JS (looking) scripting language (that and not being able to declare events). It's a stumbling block I don't think someone would run into in C#. They are trying to emulate a dynamically typed language (JavaScript / ECMAScript) in a strongly typed environment. It's just syntax that looks like JS (just like VB.net resembles VB6, but is really it's own beast that is closer to C# than the old vb). Anyhow that's neither here nor there.
Let me know if this helps. If not I'll whip out the ol' JS editor and get it working.
It worked! Thanks. I think I have some studying to do, since I don't really understand the casting part. $$anonymous$$any thanks dubbreak!
Glad to help. I fixed my other solution (preview showed that greater than a less than signs worked but I had to do the html escape for them to make 'em work).
It's definitely something confusing for someone new to program$$anonymous$$g (or typed languages). I'm really used to typed languages (and in c# it'd be silly to use a
list<object>
rather than a
list<specificType>
.. I mean there are some reasons to have a list of the base type object, but not when you are only sticking one type of object in it). Unfortunately the unity js makes things look deceptively easy, then causes headaches like this.
No worries though, you got over this hump so it most likely won't rear its ugly head again. Just part of the learning curve.
NOTE: dammit!!! escapes don't work unless in code blocks!!!
Type inference in C# works just like Unityscript, you know.
var foo = 5; // Valid C# and Unityscript code, works the same in both
Also Unityscript doesn't look like JS, it looks like ActionScript3. Or JScript.NET
(but not JScript).
Did I imply it doesn't?
The thing is .net/mono can't do implicit inference. The 5 is explicit (as a literal it's a 5 and you can for it to be a double by casting..)
var num = (double)5;
I stand corrected on it looking like js, but calling it javascript implies dynamic typing. Calling it a script implies that, which is misleading.
Var is not a dynamic type, it's an inferred static type. It gets a single type and it stays that type over it's lifetime. Not only that but it's bound at compile time (which it has to do to be type safe).
.net 4.0 now has the dynamic type. But that's a whole other kettle of fish.
Actually I kind of did by calling it a type inference issue. It isn't an inference issue at all. It's the fact that the js "Array" data structure isn't typed so all objects inserted get typed to the base type object (and I assume value types get boxed).
Your answer
Follow this Question
Related Questions
Getting an error trying to assign a value to an array of classes element 0 Answers
Problem with arrays in a list 1 Answer
adding classes to arrays 2 Answers
Why Again Arrays In ExecuteInEditMode() Gives NullReferenceException Errors?? 1 Answer
Distance error: Cannot cast from source type to destination 0 Answers