• 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 feneq · Sep 20, 2011 at 11:00 PM · gameobjectarrayarraysarraylistremoveat

Problems with Arrays

The problem isn't the array actually, the problem is finding a solution to my troubles really. Let me try and explain what I'm trying to do. First, I'm setting variables.

 public var objScout1:GameObject;
 public var objScout2:GameObject;
 public var objScout3:GameObject;

I also make a built-in array for gameobjects.

 public var wave1:GameObject[];

and in the start function I add these gameobjects to the array.

 function start(){
     wave1 = new GameObject[3];
     wave1[0] = objScout1;
     wave1[1] = objScout2;
     wave1[2] = objScout3;
 }

Elsewhere I'm instantiating each of these by use of random number,

 function selection(b){
 //b = random number based on how many objects are in the array
     var enemy = Instantiate(wave1[b], Vector3(transform.position.x,Random.Range(-   14,14),transform.position.z), transform.rotation);
     wave1.RemoveAt(b);
 }

This function takes a random number, instantiates the object and then what I would like it to do after that is, remove that same object from the array. The problem is, which I just found out a minute ago- you cannot remove objects from a built-in array because these arrays have a fixed size.

I have looked around and I found another method in which you could add gameobjects to an arraylist, the problem with that for me was that the length of the array would always came back null. Is that normal? Can anyone possibly give me some insight? Thank you.

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
0
Best Answer

Answer by Peter G · Sep 21, 2011 at 12:13 AM

This is what lists are for. They are type safe, but at the same time, they are resizable. There are a couple different types of lists, but in your case, the basic list should work well:

 import System.Collections.Generic;
 
 public var objScout1:GameObject;
 public var objScout2:GameObject;
 public var objScout3:GameObject;
 
 public var wave1 = new List.<GameObject>();
 //The GameObject between the brackets determines the type the list holds.
 
 function Start () {
     
     wave1.Add(objScout1);
     wave1.Add(objScout2);
     wave1.Add(objScout3);
 
 }

then you shouldn't have to change how your instantiate code works. You can access lists the same way as arrays with the index i.e. wave1[0].

And its easy to remove items:

  wave1.RemoveAt(i);

as described here.

Comment
Add comment · Show 1 · 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 feneq · Sep 21, 2011 at 12:38 AM 0
Share

wonderful, thank you !

avatar image
0

Answer by Steven-Walker · Sep 20, 2011 at 11:53 PM

Yes, there is a distinct difference between built-in arrays such as GameObject[] and Array(). If you want to resize a built-in array, you'll have to handle the removal of the element yourself using a custom method to rebuild the array. Here's code that should do the trick.

 function RemoveItemFromArray(i : int) {
     var newCount : int = wave1.length - 1;
     if(!newCount) {
         wave1 = null;
     }
     else {
         var newWave : GameObject[] = new GameObject[newCount];
         var y : int = 0;
         for(var x : int = 0; x < wave1.length; x++) {
             if(x != i) {
             // Skip the element at index i
                 newWave[y] = wave1[x];
                 y++;
             }
         }
         wave1 = newWave;
     }
 }
Comment
Add comment · Show 1 · 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 feneq · Sep 21, 2011 at 12:38 AM 0
Share

This is great and works. It's great because something like this did not even occur to me so it will help me down the road when finding solutions to a problem. However, I'm not entirely sure, but I believe the answer posted by Peter G. may be better suited for the task at hand. Thank you sir.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to Narrow Down an Array casted from FindObjectsOfType 1 Answer

How to merge an arrays into arrays inside an array? 3 Answers

Problems creating new List of type GameObject and adding GameObject to it. 1 Answer

How to Clone Array1 into Array2 in Unity? Using JavaScript. 0 Answers

GameObject to Array/List or whatever fits best 0 Answers


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