• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Rufalo · Mar 19, 2010 at 05:38 PM · javascriptarrays

Add an object to Array

hi i want to know how i can add an gameobject to a Array when the gameObject is created i want to have a command that add the object to a spesific array in another Script file

and if the gameObject is Desroyed remove him from the Array and fix the Array what i mean is if the Array has 5 entrys and entry 3 is Desroyed i want entry 4 to be 3 and 5 to be 4

sorry for my bad spelling :S and im Writing this in JavaScript

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Jason_DB · Mar 19, 2010 at 08:45 PM

You can pretty easily go about adding and subtracting from an array, here's an example

//this is the array you will be modifying, you will need to get a hold of it first if it is in another script var array1 : gameObject[];

// this function will need to have the object that is being added passed in to it.
function addToArray (obj : gameObject) { array1.Add(obj) }

And as far as subtracting a gameObject goes you could try removing it from the array before you destroy it.

// this function will need to have the object that is being removed passed in to it.
function destroyObject (obj : gameObject) {
    var objectIndex : int;
    // find the index of the object to be deleted (what number it is in the array)
    for (var i : int=0 ;i<array1.length; i++){
        if(array1[1] == obj)
            objectIndex = i;
    }
    //remove the object from the array
    array1.RemoveAt(objectIndex);
    //now destroy it
    Destroy(obj);
}
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gl03 · Jun 01, 2012 at 07:04 PM 0
Share

should probably be:

if(array1[i] == obj)

avatar image aman_jha · Aug 07, 2014 at 08:28 PM 0
Share

What do you do for C#?

avatar image
2

Answer by Waz · Jun 21, 2011 at 11:26 AM

There is no "Add" method on the builtin array type! The above will not compile, even if you do use "GameObject" instead of "gameObject".

However, try this:

 var array1 : GameObject[];
 
 function addToArray (obj : GameObject) {
    array1 += [obj];
 }

Voila! Note that this is less efficient than implementations such as a fixed-maxsize array with an end cursor, but I tend to use it when the adding to the array is not performance-critical (eg. bullet is created), thus making it easy to use these fast builtin arrays, so that the performance critical array-READING code (eg. for every bullet every frame, check for hits).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

JesseEtzler's RPG Talent System in C# 1 Answer

My array does not update when object is destroyed. How do I fix it? (java) 2 Answers

how to save an array into an object 1 Answer

Javascript Multidimensional Arrays 0 Answers

make scroll chat with scroll bar 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges