x


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;
}
more ▼

asked Jul 14 '12 at 12:53 PM

SirVictory gravatar image

SirVictory
1.7k 64 77 104

I wrote an answer it just has to be allowed so wait a minute

Jul 14 '12 at 02:33 PM goo-muffin
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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  ლ(ಠ益ಠლ
      }
}
more ▼

answered Jul 14 '12 at 02:54 PM

SirVictory gravatar image

SirVictory
1.7k 64 77 104

Um wouldn't that be easier as:

  void LoadHat(int index)
  {
       head = hats[index];
  }
Jul 14 '12 at 03:16 PM whydoidoit

I swear if that works, I'm gonna kill you :D

Jul 14 '12 at 03:18 PM SirVictory

And it does :I

Jul 14 '12 at 03:21 PM SirVictory

:) Somethings are easier than they look!

Jul 14 '12 at 03:22 PM whydoidoit

Yeah, I neglected to remember that a list is like an array :P

Jul 14 '12 at 03:25 PM SirVictory
(comments are locked)
10|3000 characters needed characters left
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

more ▼

answered Jul 14 '12 at 03:28 PM

goo-muffin gravatar image

goo-muffin
185 6 11

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2087
x356
x65

asked: Jul 14 '12 at 12:53 PM

Seen: 449 times

Last Updated: Jul 14 '12 at 03:28 PM