Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Find colliders after collision? 2 Answers

Problem removing items from array 2 Answers

List problem? 2 Answers

Distance error: Cannot cast from source type to destination 0 Answers

Change rigidbody values on mutliple objects (ARRAY) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges