subtracting from arrays?

Hi there,
I have an array with 2 elements. These two elements can be destroyed at any given time. They are enemies as such… kaboomski. Anyhow, my question is this…

perhaps it will help better if I give the scenario.

|||
scene:

  1. dbManager /// holds dbManagerScript, containing the dbCount Array, with an element of 2.
  2. dbBlock /// there are 2 of these in scene, each, when destroyed by hero bullets I want to pass this information (their destruction) into the dbManagerScript saying that in regards to the array dbCount, that there is one less element.

How can I do this? Or simply put, go about notifying a scriptX via scriptY that xArray now has one less elements “alive” in scene.

you could poll it every frame and remove items that are not there, and if the value changes, notify another script that things have changed.

import System.Collections.Generic;

var myArr : List.<GameObject> = new List.<GameObject>();

function Update(){
	var count = myArr.Count;
	for(var i=0; i<myArr.Count; i++){
		if(myArr[i] == null){
			myArr.RemoveAt(i);
			i--;
		}
	}
	if(count != myArr.Count) print("Changed");
}

So i am still learning the ins and outs of arrays.

Is a list different from an array?

In my game the array i am using is just a count. So, does an array have a count, or, just thought of it now, size.

Thanks.

Ps.
Thats a nice little piece of code.
Thanks.

Lists are nice. You can treat them as arrays… myList[indexLocation] I like to work with them over arrays because the size of Lists are easily expandable, or as some say Dynamic. Simply doing an add will make the list count larger.

yah that seems to make sense.

I have a question tho.
I just pasted in the code provided by the BigMisterB, and there is an error (2 actually), a simple syntax…
but in theory, I just have a few questions regarding scripting lists.

a. Unity arrays and regular arrays are different. A trick I learned was to define a regular array then pass that into a Unity array. Same thing with lists?

b. how do I define a list?
var myList : list;

Lists are type safe whereas UnityScript Arrays are not. When you declare a List you should also provide the type of the list.

List<string> listOStrings = new List<string>();

void Foo()
{
    string s = "Hi there!";
    int i = 100;
    // works
    listOStrings.Add(s);
    // throws an exception
    listOStrings.Add(i);
}

Sweet thanks.

I just realized tho, and please jump in if this is just plain bad programming but I see that I can use myArray.length, set a new var to reflect that length, and adjust that new var.

so …

myArrayLength = myArray.length;

if (something killed)
myArrayLength–;

code fixed above, I forgot () in the declaration of myArr. KelsoMRK’s is how to define it in C# mine is how to in US… Remember that in both you need to do using or import System.Collections.Generic or it wont work at all.

ok great.
thank you.

You can do this but it doesn’t modify the underlying collection. Nor would directly modifying the length property as its read only. BigMisterB’s method is the correct one for removing an item from a collection “OnDeath” as it were.