- Home /
using Contains(gameObject) to find and destroy a gameObject from a list
I'm not really sure whats going on, finding and destroying a gameObject using the same code works in another part of the game - I'm sure it's something obvious but I just can't see it.
Here's the code to generate the key:
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class keyIssuer : MonoBehaviour {
 
     public static List <GameObject> myOnePercentObj = new List<GameObject>();
 
     public static int key = 0; 
 
     void Start()
     {
         //with list
         Object[] onePercentObj = Resources.LoadAll("onePercent", typeof(GameObject));
         
         
         foreach (GameObject subOnePercentObj in onePercentObj) 
         {    
             GameObject hi = (GameObject)subOnePercentObj;
             
             myOnePercentObj.Add(hi);
             
         }
 
         float waitForIt = Random.Range (0f, 10f);
 
         Invoke("KeyIssuer", waitForIt);
         
     }
 
     void KeyIssuer() 
     {    
 
         int onePercent = Random.Range (0, 99);
 
 
         if (onePercent <= 60) 
         {
             myOnePercentObj.TrimExcess();
             //spawns item in array between position number 0 thru myOnePercentObj.Count
             int onePercentItem = Random.Range (0, myOnePercentObj.Count);
             
             GameObject oneObj = Instantiate (myOnePercentObj [onePercentItem]) as GameObject;
             
             oneObj.transform.position = transform.position;
 
             
 
         }
         
     }
 
 }
and here's the code used when the player clicks on the key:
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 
 public class destroyMouse : MonoBehaviour {
     
     void OnMouseDown()
     {
         if (keyIssuer.myOnePercentObj.Contains(gameObject))
         {
             Debug.Log (keyIssuer.myOnePercentObj.ToString());
             
             keyIssuer.key++;
             Destroy(gameObject);
             Debug.Log ("You found a Key!");
             
         }
         }
 }
 
I think it might have something to do with the original GameObject in the list is named '$$anonymous$$ey' and the instantiated Game Object is named '$$anonymous$$ey(clone)' - I got rid of '(clone)' with a line:
         oneObj.name = oneObj.name.Replace ("(Clone)", "");
but it still treats it as a separate object.
Answer by glasstongue · Jun 01, 2014 at 08:17 AM
I was hoping to keep it in one script but since there should be less than 10 special objects I decided to just write a script for each one. Here is my solution:
 using UnityEngine;
 using System.Collections;
 
 public class destroyKey : MonoBehaviour {
 
     
     void OnMouseDown()
     {
         SoundEffectsHelper.Instance.MakeKeySound();
         Destroy(gameObject);
         keyIssuer.key++;
         Debug.Log ("You found a Key!");
     }
 
 }
Answer by nkvnkv · Oct 03, 2014 at 11:17 PM
Just a taught, Each GameObject in list to have a script with this code;
 public static int  id;
 
 private void Awake(){
 id++;
 }
Each created GameObject will in List will have different id from 0 - int upper limit described at http://msdn.microsoft.com/en-us/library/296az74e.aspx
So you do not need to worry about (Clone) postfix.
To Find and Destroy Game Object, Keep in mind that I have written this on the fly.
 public bool SearchAndDestroy(int myId, List <GameObject> list){
  foreach (GameObject go in list)
 { 
 if(go.getComponent<ScriptWithId>().id == myId){
 Destroy(go);
 return true;
 }
 return false;
 }
 }
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                