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 THDevelopment · Jul 03, 2014 at 01:51 PM · arrayarray of gameobjects

Delete first Element on Array (JS)

Hey Guys! For many people its a cheap question but i dont know how to handle this:

I created a Trigger which spawns Obstacles if the player goes trough - after he enters another one this trigger stop spawning new Obstacles. So i put all the new spawned Obstacles into an array, because i want to delete them after X time (because of memory overload). The Question is:

How can i delete the first Obstacle which is on the first spot in the array (i hope iam right if i think its the first and oldest Obstacle i spawned)?

 var spawnerHotSpot:Transform;
 var spawnObject:GameObject;
 var arraySpawnedObjects:Array; 
 
 var deleteInterval:int; 
 private var deleteTimer = 0.0; 
 
 var spawnInterval:int; 
 private var spawnTimer = 0.0; 
 
 
 
 public static var spawnActive:boolean = false; 
 
 
 function Start () 
 {
     
 }
 
 function Update () 
 {
     spawnTimer += Time.deltaTime;
     
     arraySpawnedObjects = GameObject.FindGameObjectsWithTag("Obstacle"); 
 
     if(spawnActive == true)
     {
         if(spawnTimer >= spawnInterval)
         spawnObstacle(); 
     }
     
     for(var dO  : int = 0; dO < arraySpawnedObjects.length; dO++)
     {
         if(deleteTimer >= deleteInterval)
         {
             arraySpawnedObjects.remove(arraySpawnedObjects[dO]);
         }
     }
     
 }
 
 function spawnObstacle ()
 
 {
     Instantiate(spawnObject, spawnerHotSpot.position, spawnerHotSpot.rotation); 
     spawnTimer = 0.0;
 } 

(there are two other scipts but they arent necessary for this question - so pls ignore some variables which are not instanced in this part like spawnActive.)

Thank you for you answers!

Comment
Add comment · Show 1
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 HarshadK · Jul 03, 2014 at 01:57 PM 1
Share

Rather than arrays you can use List which is better to handle and also efficient than JS arrays.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Jul 03, 2014 at 04:03 PM

First, you are using the Array class. It is untyped and slow, and not recommended. But if you are using it, there is a RemoveAt() function in the class. Note this removes the entry from the array. It does not destroy the game object (which is what I believe you want). GameObject.FindGameObjectsWithTag() returns a built-in array. I don't see any reason in this code to make 'arraySpawnObject' a class instance variable. It can be local to the function. So you can change line 25 to:

 var arraySpawnObjects = GameObject.FindGameObjectsWithTag("Obstacle"); 

Or:

 var arraySpawnObjects : GameObject[] = GameObject.FindGameObjectsWithTag("Obstacle"); ;

No Remove() function exists for the built-in array class. It is possible to copy all the elements down and resize a built-in array but it is not what you want here. I believe all you want to do is:

 if(deleteTimer >= deleteInterval)
 {
     Destroy(arraySpawnedObjects[0]);
 }

Note that is likely that the first element is the oldest, but I don't believe anything in the API references guarantees the order. And if it the order is correct now, it is possible that it will change in some future version of Unity. And you don't need to worry about deleting the array entry because 1) it is generated anew each frame, and 2) it is only a reference to the game object and therefore very small.

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

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Wrong object always Instantiated 1 Answer

How to make an array list set to false gradually instead of instantly? 2 Answers

I want to add cloned gameobjects that are triggered, to an array. 2 Answers

Copy Childen Of GameObjects From Array 1 To Array 2 1 Answer

Array along on Z axis and along X axis. 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