Variable resetting back to zero


I’ve been trying to step through a define array size, but for some reason everytime I destroy an object in the array, it’s resetting my variable back to zero. Here’s the code

function OnMouseDown(){
    		if(PlayerCCC <  GlobalVariables.ColourChangeSel.Length){
    		Debug.Log("PLAYERCCC = " + PlayerCCC + " < CCS.Length = " + GlobalVariables.ColourChangeSel.Length);
    			if(GlobalVariables.ColourChangeSel[PlayerCCC] != null){
    			gameObject.renderer.material.SetColor("_Color", GlobalVariables.ColourChangeSel[PlayerCCC].renderer.material.color);
    				PlayerCCC += 1;
    				Debug.Log("NOT NULL. PLAYER CCC = " + PlayerCCC);

Can anyone tell me why this is happening or offer a solution? I’ve tried searching the internet and re-read the unity Script Referencing.


Sorry about the delay in getting back to you all. A broken computer has slowed me down for the last week. I’ve figured out my problem.

This script was attached to several nodes in my game and on all of the nodes it was creating a new variable for each node instead of creating a single node (PlayerCCC) for all the nodes. After changing the variable from local to global I’ve fixed my problem. Thanks everyone for the input and help.

Depending on your version, you may be experiencing a bug: