Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Nercoe · Oct 28, 2012 at 02:19 PM · arrayremoveitem

Remove item from array

Hey, I have seen various questions on this but can only really find C# scripts which to me is no help. I have a script that instantiates enemies at run time at different spawnpoints (gameobjects). What I need is when and enemy is spawned at a gameobject, I need the gameobject to be destroyed (from the array) so that multiple enemies do not spawn on top of each other. Here is what I have:

 var chicken:GameObject;
 var spawn:Transform[];
 
 function Start(){
 var spawnpos:Transform = spawn[Random.Range(0,spawn.length)];
 Instantiate(chicken,spawnpos.position, spawnpos.rotation);
 
 }


// In a nutshell, if enemy spawns at spawnpoint1, delete spawnpoint 1 so enemy2 cannot spawn there. Any help/advice is appreciated guys :)

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

Answer by phodges · Oct 28, 2012 at 03:13 PM

There are all sorts of possible ways of doing this. An approach would be to do pretty much what you are already and then after each spawn event allocate a new, shorter array and copy results back into that. The overhead for that is unappealing though, so let's do something else entirely. What this sample will do is leave the initial array intact and instead build the whole random sequence during Start.

 #pragma strict
 import System.Collections.Generic;

 var prefab: GameObject;
 var points : Transform[];
 
 private var spawnIndex : int;
 private var sequence : Transform[];
 
 function Start () {
     var totalPoints : int = points.Length;
     sequence = new Transform[totalPoints];
     var indices : List.<int> = new List.<int>(totalPoints);
     // Stock the array with the whole set of possible points
     for (var i : int = 0; i < totalPoints; ++i){
         indices.Add(i);
     }
     // Now randomly select from this list, removing items from it and filling the sequence
     var random : int;
     var selectedIndex : int;
     var sequenceIndex : int;
     while ( totalPoints ){
         // Pick a point out of the set of indices
         random = Random.Range(0, totalPoints);
         selectedIndex = indices[random];    
         sequence[sequenceIndex] = points[selectedIndex];
         ++sequenceIndex;
         // Now we've made use of this index, prevent it from being selected again
         indices.RemoveAt(random);
         // Less points available, make note of that so the next random selection is valid and that we exit the loop correctly.
         --totalPoints;
     }
 }

// Call this each time you want to spawn a new item.

 function SpawnOne() {
     if (spawnIndex < sequence.Length){
         var at : Transform = sequence[spawnIndex];
         if (null != prefab){
             Instantiate(prefab, at.position, at.rotation);
         }else{
             Debug.LogError("No prefab to spawn");
         }
         ++spawnIndex;
     }else{
         Debug.Log("Spawn points are exhausted.");
     }
 }

// Debug method - so you can see what is going on.

 function OnGUI() {
     GUILayout.BeginArea(Rect(10f,10f,120f,120f));
     GUILayout.BeginVertical();
 
     for (var i : int = 0; i<sequence.Length; ++i){
         if (i == spawnIndex - 1){
             GUILayout.Label(String.Format("{0}] {1} <--- last spawn", i, sequence[i].name));
         }else{
             GUILayout.Label(String.Format("{0}] {1}", i, sequence[i].name));
         }
     }
 
     GUILayout.EndVertical();
     GUILayout.EndArea();
 }
Comment
Add comment · Show 3 · 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 Eric5h5 · Oct 28, 2012 at 03:59 PM 0
Share

Don't use the JS Array class, it's slow and obsolete. Use List ins$$anonymous$$d.

avatar image phodges · Oct 28, 2012 at 04:08 PM 0
Share

A more than reasonable point. For my own work I use C#, so it was interesting to see how the generic types are imported into JS.

avatar image alone1992 · Feb 22, 2014 at 07:20 AM 0
Share

Excuse me I have problem !! $$anonymous$$y unity3d say EOF import !? have problem with import when I type import System.Collections.Generic;

avatar image
0

Answer by Eric5h5 · Oct 28, 2012 at 04:05 PM

Use a List. C# answers will help you in most cases since both languages have most of the same stuff available; just translate to Unityscript syntax. (Unless you don't know C# at all, but they're actually more similar than they are different so it doesn't take much effort.)

 import System.Collections.Generic;
 
 var chicken : GameObject;
 var spawn : List.<Transform>;
 
 function Start() {
     var itemNumber = Random.Range (0, spawn.Count);
     var spawnpos = spawn[itemNumber];
     spawn.RemoveAt (itemNumber);
     Instantiate (chicken, spawnpos.position, spawnpos.rotation);
 }
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 Nercoe · Oct 28, 2012 at 04:16 PM 0
Share

Thanks for this but the project is stricty to be produced in JS, although this is solid script, I am "unfamiliar" with the API. What I wish to do is stop my enemies from spawning on top of each other, would this even fix that problem?

avatar image Eric5h5 · Oct 28, 2012 at 07:25 PM 0
Share

Being produced in JS doesn't prevent you from using List...that is in fact what you should use. The code I wrote is JS. It answers your question about removing an item from the array. You really should become familiar with the .NET API, because that's what Unity uses, regardless of language.

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

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to delet used array item 2 Answers

Setting up an array of Items 1 Answer

Preventing Items from shifting in a list 1 Answer

Can't Assign Item In Array 1 Answer

Remove an object from an array and destroy it (C#) 2 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