I’ve done this many times before… in javascript.
Now I’m trying it on c# exactly like the many examples telling me:
Inv.ItemList.Add(this.gameObject);
In the Inv script, the list is defined that way:
public List ItemList = new List();
System.Collections.Generic; is in use, the scripts are conected.
Neither the list nor the object is used in any other way, and exists at the point of action.
But I just get the note:
NullReferenceException: Object reference not set to an instance of an object
If you need to do multiple values, you can store anything in this kind of list:
public List<object> MyList = new List<object>();
It probably takes up must more memory, though.
The examples given should in fact work… I just wrote one today I’ll show you my code.
This is MY code, but this does in fact allow you to add an object to a list, and as shown at the bottom, how to destroy all the objects within the trigger. (This script is used for building placement - to remove flowers, etc).
private List<GameObject> obList = new List<GameObject> ();
void OnTriggerEnter(Collider col){
if (col.gameObject.tag == "Vegitation" && isEnabled) {
obList.Add (col.gameObject); // << Do this to add Obj to list.
foreach (GameObject obj in obList) {
Destroy (obj); // Now I destroy the objects in my trigger.
}
Destroy (GetComponent<Rigidbody> ());
GetComponent<CheckForVegitation> ().enabled = false;
}
}