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 /
  • Help Room /
avatar image
0
Question by dsamuelson · Aug 29, 2015 at 01:56 PM · gameobjectlistdestroy

How can I destroy a Gameobject from a list at a specific index

Ok so I'm trying to simplify a random level generator. in my scene I have a couple rooms which will be called to instantiate at a certain position when the trigger is passed. The generation code works fantastic but it all get's added to a list that I then want to destroy the gameobject at index 0 of the list when the index gets to 3. I can seem to remove it just fine from the list but no matter what I try I can't seem to destroy that game object.

here's my script

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class IRGenScript : MonoBehaviour {

 public GameObject[] availableRooms;
 public List<GameObject> currentRooms;
 public GameObject roomtoRemove;
 private float roomWidth;

 private int myLayerMask = 1 << 10;
 private Vector3 positionRoom;


 void Start ()
 {

 }

 void Update()
 {

 }
 void OnTriggerExit2D (Collider2D generator)
 {
     Destroy (generator.gameObject);
     RaycastHit2D hitdown = Physics2D.Raycast ((GameObject.Find ("Camera target").transform.position) , Vector2.down,Mathf.Infinity, myLayerMask);
     if (hitdown.collider != null)
         positionRoom.y = hitdown.collider.transform.position.y;


         if (generator.transform.tag == "Room End") {
             int randomRoomIndex = Random.Range (0, availableRooms.Length);
             GameObject room = (GameObject)Instantiate (availableRooms [randomRoomIndex]);
             room.transform.position = new Vector2 (transform.position.x, positionRoom.y + 2);
             currentRooms.Add (room);
             List<GameObject> roomsToRemove = new List<GameObject> ();
         if (currentRooms.Count.Equals (3))
             currentRooms.RemoveAt (0);
     




     }

 }

} and it's connected to the player.

Comment
Add comment · Show 2
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 dsamuelson · Aug 28, 2015 at 07:29 PM 0
Share

as well my player has a gameobject pasted to it for the camera target. I did this because I'm using free floating sprites to represent the player and if I don't then the camera seems to ignore most of them I'm aware of how to fix it so no worries I just find it a bit easier this way

avatar image legion_44 · Aug 29, 2015 at 03:12 PM 1
Share

You're first calling Destroy on the generator object and then You check it's tag (lines 21 and 26). to destroy the room just Call Destroy(currentRooms[index]) before removing it from list (line 33).

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by dsamuelson · Aug 31, 2015 at 02:46 PM

fapawel · yesterday 1 Unlike Share You're first calling Destroy on the generator object and then You check it's tag (lines 21 and 26). to destroy the room just Call Destroy(currentRooms[index]) before removing it from list (line 33).

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 dsamuelson · Aug 31, 2015 at 02:48 PM 0
Share

$$anonymous$$y new script looks like this

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class IRGenScript : $$anonymous$$onoBehaviour {

 public GameObject[] availableRooms;
 public List<GameObject> currentRooms;
 public GameObject[] roomToRemove;
 //public GameObject roomtoRemove;
 private float roomWidth;



 private int myLayer$$anonymous$$ask = 1 << 10;
 private Vector3 positionRoom;


 void Start ()
 {

 }

 void FixedUpdate()
 {

     RaycastHit2D hitdown = Physics2D.Raycast ((GameObject.Find ("CreateNext").transform.position) , Vector2.down,$$anonymous$$athf.Infinity, myLayer$$anonymous$$ask);
     if (hitdown.collider != null) {
         positionRoom.y = hitdown.collider.transform.position.y;
         roomWidth = transform.position.x;

     }
     if (hitdown.collider == null) {
             int randomRoomIndex = Random.Range (0, availableRooms.Length);
             GameObject room = (GameObject)Instantiate (availableRooms [randomRoomIndex]);
         room.transform.position = new Vector2 (roomWidth, positionRoom.y + 1.9f);
             currentRooms.Add (room);
         if (currentRooms.Count.Equals (3)){
             Destroy (currentRooms[0]);
             currentRooms.RemoveAt (0);
         
         }


     }

 }

}

Which is attached to an empty game object called create next

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Shake GameObject in 0 Answers

How to add a Gameobject to a list and remove it from Game World? 1 Answer

Destroyed instance of Prefab, can't spawn it back. 1 Answer

Get a selected item from a GameObject list? 0 Answers

I can't destroy my instantiated game object 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