- Home /
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
}
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;
}
}
Cool, thanks for the reply. I didn't know how to handle the array O.Sphere returned. Thanks for the help!
$$anonymous$$y understanding of for loops must be off. wouldent "proxNode" just be a number? shouldent it be proxNodes(proxNode).etc........?
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++)
I did not know there was a diffrence in that respect. I thought it just was shortcut. Thanks for the details.
How do I mark this question as answered? This has helped me much. For/in loops are very powerful and save so much code!
Your answer
Follow this Question
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