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 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.

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

Order of elements in array going back to default after being set in different order 1 Answer

Array index out of range 1 Answer

Array sorting issues - Please Help!!! 4 Answers

Is it possible to choose some prefabs from an array in a procedural way? getting a seed to recreate the selection?. 1 Answer

Check if transform.position matches position of any object in a given array 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