- Home /
Accessing script variables in objects in an array.
Hello and thanks for you time.
I am trying desperately to access variables in an array of game objects but haven't been able do it. The below code snippet it supposed to go through the GO's in the array and declare the go with he highest bestBubValue to be the bestBubble.
for (i = 0; i < bubArray.length; i++) { if (((Bubble)bubArray[i]).bestBubValue >= currentBestValue) { bestBubble = (Bubble)bubArray[i]; currentBestValue = ((Bubble)bubArray[i]).bestBubValue; }
}
I think I need to cast it somehow but don't understand how it works (or really what "casting" even is.) Save me Superman!
Answer by Goody! · Mar 05, 2011 at 04:20 AM
Well, by parroting this answer I ended up with this code which seems to work:
for (var bub : GameObject in bubArray) { bubbleScript = bub.GetComponent(Bubble); bubbleScript.setBestBubValue();
if (bubbleScript.bestBubValue >= currentBestValue)
{
bestBubble = bub;
currentBestValue = bubbleScript.bestBubValue;
Debug.Log("THIS IS THE BEST BUBBLE " + bestBubble.name);
}
}
I guess I casted? :)
"Casting" is declaring the type of a variable. IE: the only variable you cast in the example is bub:GameObject. Not a big deal as JavaScript will imply it for you if you don't, C# won't, though.
Your answer
Follow this Question
Related Questions
Convert Array into One String (Js) 1 Answer
Help me Convert JS to C# 2 Answers
+= doesn't work with a 2d array in JS 1 Answer
Adding an object to an array of custom objects (JS) 1 Answer
For Loop Array Problem 1 Answer