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!");
}
}
}