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*;*

_ Debug.Log(countryNodes*);_
_
//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*.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?

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*; //helps type inference along*

Debug.Log(countryNode.nameCountry);
}
Other solution: Use a typed structure.
var countryCodes : List. = new List.();

//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.