Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by beanslyjones · Sep 15, 2012 at 09:28 PM · colliderarraynewbie

Error when declaring Array

Hi guys another Newbie here thanks in advance for helping...

I am trying to assign an : Collider array element before comparing its components. I am getting the "It is not possible to invoke an expression of type 'Array'." error. I have read that error is misleading and usually means my systax is incorrect or I am missing something...

The documentation notes that Physics.OverlapSphere() returns colliders, so I am declaring a :Collider var and from that I should be able to get a gameobject, and thus access the gameobjects.GetComponent(str) var I want to compare.

Do I even need to assign the collider from the array or can I keep it in there?

 function GetStr (){        //Get the closest 5 nodes including current node, get the str of all of them and return 
                         //the highest. Get the highest nodes gameobject.transform. save the gameobjects transform 
                         //in an waypoint array. move to the next node. Untill reaching destination. 
                         
                         //Advanced - (Disp[lay all routes to Destination. cull to the top 3 routes. Then check for
                         // traffic and choose route computed with traffic.
                         //In order to choose multiple routes I will have to mark used nodes to not be used again in 
                         //future loops. ( this will need a function to give a value to intersections to be used multiple times)
                         // perhaps a value of how many beighboring nodes. so a street will have a node left and right 
                         // while a intersection will have 3 or 4 depending on the cross roads... 
                     
     
     var strongest    :int = 0;     // set the lowest node to compare with (to be updated with curStr when availiable)
     var curStr         :int;        // the current node being analyze
     var proxNodes = Array (Physics.OverlapSphere(transform.position, range));    // get collider of nearby nodes
                                                                                  
     for (var i in proxNodes){                                                    // loop through the node array
             var curArray:Collider = proxNodes(i);                                // Declare and assign curArray as type :Collider
                 if (curArray.GetComponent.str)                                    // get around null , probley not needed but good habit
                      curStr = proxNodes(i).GetComponent.str;                    // assign the str var from each node ot be tested
             
                     if(curStr > strongest) strongest = curStr;                    // compare each str value and replace the highest value ot "strongest 
         
         
         
     }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Eric5h5 · Sep 15, 2012 at 09:48 PM

You don't want to use Array. Physics.OverlapSphere already returns a Collider[] array, as you can see in the docs, so just do

 var proxNodes = Physics.OverlapSphere(transform.position, range);

Also, that's not a correct use of a for/in loop, and you must supply the name of the Component you're actually getting when using GetComponent.

 for (var proxNode in proxNodes) {
     var script = proxNode.GetComponent(InsertCorrectNameHere);
     if (script) {
         if (script.str > strongest) strongest = script.str;
     }
 }
Comment
Add comment · Show 6 · 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 beanslyjones · Sep 15, 2012 at 09:52 PM 0
Share

Cool, thanks for the reply. I didn't know how to handle the array O.Sphere returned. Thanks for the help!

avatar image beanslyjones · Sep 15, 2012 at 09:58 PM 0
Share

$$anonymous$$y understanding of for loops must be off. wouldent "proxNode" just be a number? shouldent it be proxNodes(proxNode).etc........?

avatar image Eric5h5 · Sep 15, 2012 at 10:03 PM 0
Share

That's a for/in loop, which is not a for loop, and does not use an index number. A for loop is like this:

 for (var i = 0; i < proxNodes.Length; i++)
avatar image beanslyjones · Sep 15, 2012 at 10:15 PM 0
Share

I did not know there was a diffrence in that respect. I thought it just was shortcut. Thanks for the details.

avatar image beanslyjones · Sep 15, 2012 at 10:44 PM 0
Share

How do I mark this question as answered? This has helped me much. For/in loops are very powerful and save so much code!

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.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

Destroy objects within ordered Array by line of sight 1 Answer

Add scripts from Resources? 0 Answers

How To Get A Reference To All Nearby GameObjects? 1 Answer

Storing collided gameObject array index 1 Answer

Adding a collider to array 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