• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Sicco · Feb 14, 2013 at 02:34 PM · arrayarraysclassmember

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?

Comment

People who like this

0 Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Feb 14, 2013 at 05:46 PM 1
Share

It would be really a lot better if you forget about using Array. Use a generic List instead, it's faster/better in every way and you won't have casting problems.

avatar image dubbreak · Feb 14, 2013 at 05:53 PM 0
Share

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;
avatar image Sicco · Feb 15, 2013 at 01:17 PM 0
Share

Thank you, I will remember that.

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

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.&lt;CountryNode&gt; = new List.&lt;CountryNode&gt;();
 
 
 //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.

Comment

People who like this

0 Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sicco · Feb 14, 2013 at 05:21 PM 0
Share

It worked! Thanks. I think I have some studying to do, since I don't really understand the casting part. Many thanks dubbreak!

avatar image dubbreak · Feb 14, 2013 at 05:31 PM 0
Share

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 programming (or typed languages). I'm really used to typed languages (and in c# it'd be silly to use a

 list&lt;object&gt; 

rather than a

 list&lt;specificType&gt;

 

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

avatar image Eric5h5 · Feb 14, 2013 at 05:50 PM 0
Share

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

avatar image dubbreak · Feb 14, 2013 at 06:06 PM 0
Share

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.

avatar image dubbreak · Feb 14, 2013 at 06:09 PM 0
Share

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

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How do i edit a constructor array in the inspector? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges