• 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 Ego65 · Feb 26, 2016 at 08:18 AM · arraydetectiondeletearray of gameobjectsarray list

how to RemoveAt

I'm (still) a beginner and i was wasting hours to figure out how to use it. so i thought i give a basic example of it for any other beginners who are searching here in "Answers"

to be edited: at the moderators: i hope it will be ok if i answer my own question.

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 Ego65 · Feb 26, 2016 at 08:49 AM

first of all ; make sure that , using System.Collections.Generic; is at top !
make 2 or more different primitives, place them where everyou want, scale and rename them.
Tag them, in my case i used 2 Tags: Enemy + Obstacles

here's my examplescript in which i placed Game Objects with different tags in 1 List!

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 
 public class FindOthers : MonoBehaviour {
 
     public GameObject[] Obstacles; // Obstacles = the name of this Game Object array
     public GameObject[] Enemies;
     public List<Transform> AllOthers;
 
     public GameObject thisobj;
 
     // Use this for initialization
     void Start () 
     {
         FindObstacles();
     }
 
     void FixedUpdate () 
     {
         UpDateList_AllOthers();
     }
         
     void FindObstacles()
     {
         Obstacles = GameObject.FindGameObjectsWithTag("Obstacle");
         Enemies = GameObject.FindGameObjectsWithTag("Enemy");
     }
 
     void UpDateList_AllOthers()
     {
         // declaring AllOthers as new List is necessary !!!
         AllOthers = new List<Transform>(); // wichtig !! um Liste bearbeiten zu können !
 
         foreach(GameObject enemy in Enemies)// enemy is the name of each game object in the Game Object array
         {
             // damit wird ausgeschlossen,daß dieses Enemy Objekt in AllOthers Liste aufgenommen wird !!
             // i use this because i will attach the script to any npc enemy and so 
             // this prevents that this public game object (= thisobj) is added 
 
             Vector3 dist = thisobj.transform.position - enemy.transform.position;
             // so dist.sqrMagn. isnt really necessary but enemy.activeSelf
             if( dist.sqrMagnitude > 0.0f && enemy.activeSelf)
             {                
                 AllOthers.Add(enemy.transform); // this adds all objects tagged as "Enemy"
             }
         }
 
         foreach(GameObject obstacle in Obstacles)// obstacle is the name of each game object in the Game Object array
         {
             if(obstacle.activeSelf)
             {
                 AllOthers.Add(obstacle.transform); // this adds all objects tagged as "Obstacle"
             }
         }
 
         foreach(Transform tr_allothers in AllOthers) // tr_allothers is the name for all Transforms added to the List
         {
             // the position of all transforms of this list in world space
             Vector3 AllOtherspos = new Vector3( tr_allothers.position.x, tr_allothers.position.y, tr_allothers.position.z);
 
             Debug.Log("pos = " + AllOtherspos + " " + "scale = " + tr_allothers.localScale);
 
             if(tr_allothers == null)
             {
                 AllOthers.RemoveAt(tr_allothers.GetSiblingIndex());// this removes this transform
             }
         }
     }
 }  

attach the script to an empty game object or to an enemyobject. in playmode activate/deactivate the objects. you should see that any object which is deactivated disappears off the list and if reactivated it's in the list again.

hope this is helpfully and it works like it should ( for me its working fine!)
if so and/or you like it as well if anyone has some more advanced suggestions leave me a comment.

yours

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

Answer by Ego65 · Feb 28, 2016 at 08:02 AM

I went on and figured out how to get the index of each object in the List AllOthers;
therefore i've added 4 new variables

 private Vector3 Abstand;        //Abstand = Distance  
 public float min_Abst = 5.0f;  //is kind of a sensor with a range of 5.0f  
                                                     //(depends on  your game objectssize)  
 private int myindex;
 private int GetSiblingIndex;  
 //i changed the public game object: thisobj  
 //to public Transform = thistransform !  

first of all : leave some space between each object because detecting if they are closer than min_Abstand or even overlapping isnt part of the script up to now.

right after the foreach(Transform tr_allothers in AllOthers) loop in the UpDateList_AllOthers() function i've added :

         for (int i = 0; i <= AllOthers.Count-1;i++)
         {            
             GetSiblingIndex = i;
             
             Abstand = thistransform.position - AllOthers[i].position;
             if(Abstand.magnitude <= min_Abst)
             {
                 Debug.Log("Warning ! " +"pos = "+ i+ " = "+Abstand);
             }
         }  







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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Related Questions

Destroy question if its already asked 1 Answer

How to compare a position of an object within an array 1 Answer

create array of specific gameobjects 0 Answers

Same for each loop with multiple arrays? 1 Answer

AI to shoot other AI 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges