x


Public Array of GameObjects

Hey

I'm making a Multiplayer FPS and now i have a problem with my Weapons-Pickup-Script. In my game you can pickup weapons kinda like in actual fps like call of duty. So when you pickup a weapon you throw your actual equiped gun on the floor. For this reason i would like to have an array of GameObjects. It should be public so that i can drag my PickupWeaponsmodels right in it from the editor.

I searched for that a few times and find ArrayList but i didn't manage to get a public arraylist done where i can drag my models in. I don't even think thats possible. I also can't add my Models through tags to such an arraylist because the tags of the models are used elseways.

Hope someone finds a way to solve my rather special question.

more ▼

asked Apr 26 '12 at 04:27 PM

ExTheSea gravatar image

ExTheSea
1.9k 12 22 30

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Use builtin arrays:

C#

public GameObject[] myObjects;

or JS:

public var myObjects : GameObject[];

Now you can access them in your script once you have them set. In the inspector you need to set the number of elements you need and then drag your game objects to each of your elements. Access is done the regular way arrays are accessed. E.g.

function Start()
{
   for(var go : GameObject in myObjects)
   {
      Debug.Log(go.name);
   }

   //or like this
   for(var i:int=0; i<myObjects.Length; i++)
  {
      Debug.Log(myObjects[i].name);
  }
}
more ▼

answered Apr 26 '12 at 04:55 PM

GuyTidhar gravatar image

GuyTidhar
2.2k 4 8 13

Thank you very much i already tried the same thing before asking this question but it didn't work. Now it tried it again exactly like you described and it worked.

Apr 26 '12 at 05:19 PM ExTheSea

glad to have helped mate.

Apr 26 '12 at 06:30 PM GuyTidhar
(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:

x2077
x1356
x140
x74
x4

asked: Apr 26 '12 at 04:27 PM

Seen: 2164 times

Last Updated: Apr 26 '12 at 06:30 PM