- Home /
Assign a GameObject to one from a List<>
It seems easy enough, but I might be over-complicating things... (again).
Basically, I'm trying to make a function that will replace a GameObject with one that is either next or previous in a list of GameObjects. I couldn't fully understand the few methods in the MSDN reference I thought that would help (IndexOf, FindIndex, etc).
Psuedo Code:
List<GameObject> hats;
GameObject hat;
void ChooseNextHat()
{
hat = GetNextObject(hats); //Return the next hat in the list
}
GameObject GetNextObject(List<GameObject> equipment)
{
GameObject e = equipment.AFunctionThatReturnsTheNextIndexedObject++;
return newEquip;
}
I wrote an answer it just has to be allowed so wait a $$anonymous$$ute
Answer by SrBilyon · Jul 14, 2012 at 02:54 PM
I believe I found a way:
void LoadHat(int index)
{
int counter = 0;
hatIndex = index;
//Check the hat array. I need my hat!!!
foreach(GameObject h in hats)
{
if (counter == hatIndex)
{
hat = h;
return; //Stop, we got the right hat
}
counter++; //Still don't have the hat, run it again ლ(ಠ益ಠლ
}
}
Um wouldn't that be easier as:
void LoadHat(int index)
{
head = hats[index];
}
Yeah, I neglected to remember that a list is like an array :P
Answer by goo-muffin · Jul 14, 2012 at 03:28 PM
var selectedNumber :int; var Hats = List ; var selectedHat :GameObject;
function ChooseNextHat() { selectedHat = Hats[selectedNumber + 1]; selectedNumber ++; }
function ChooseLastHat(){ selectedHat = Hats[selectedNumber - 1]; selectedNumber --; }
This should do it. I haven't tested it yet
Your answer
![](https://koobas.hobune.stream/wayback/20220613074157im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How to add and access objects in array? 1 Answer
Get first gameobject in a list and cycle through on keypress 1 Answer
How to remove objects from a list ? 3 Answers
Ordering a list of GameObjects 3 Answers