- Home /
Variable resetting back to zero
Hi,
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);
Destroy(GlobalVariables.ColourChangeSel[PlayerCCC]);
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.
Thanks
What variable get set to zero? PlayerCCC? If so, are there other places in the script that manipulate PlayerCCC? Where is PlayerCCC defined?
Sorry, should have been more clear. Yes, it's PlayerCCC that's being set to 0. It's a local variable that I declared at the start of the script. No other function calls or changes PlayerCCC.
I can see nothing in here which should set your variable back to 0. Where are you declaring PlayerCCC? $$anonymous$$y best guess is that at some point during execution of your code it's rerunning your variable definition, resetting it to 0.
Hmmm. I can't see anything, here's the complete function
pragma strict
//////////////////////////////////////////////////////////////// //////////////////VARIABLE CREATION///////////////////////////// ////////////////////////////////////////////////////////////////
//Variable for random colour selection var RandomColour : float;
var PlayerCCC : int;
//////////////////////////////////////////////////////////////// //////////////////GA$$anonymous$$E START//////////////////////////////////// ////////////////////////////////////////////////////////////////
function Start () {
GlobalVariables.ColourArray[0] = Color.red; GlobalVariables.ColourArray[1] = Color.yellow; GlobalVariables.ColourArray[2] = Color.blue; GlobalVariables.ColourArray[3] = Color.green;
renderer.material.SetColor("_Color",GlobalVariables.ColourArray[Random.Range(0,GlobalVariables.ColourArray.Length)]); }
//////////////////////////////////////////////////////////////// //////////////////GA$$anonymous$$E UPDATE/////////////////////////////////// ////////////////////////////////////////////////////////////////
function Update () {
}
function On$$anonymous$$ouseDown(){ 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); Destroy(GlobalVariables.ColourChangeSel[PlayerCCC]); PlayerCCC += 1; Debug.Log("NOT NULL. PLAYER CCC = " + PlayerCCC); } else{ PlayerCCC += 1; Debug.Log("NULL.PLAYER CCC = " + PlayerCCC); } } }`
I don't see anything obviously wrong. If you comment out the destroy does it work? I noticed you do not initialize the variable. Is it initialized in the inspector or does some other script initialize it? Are you seeing errors in the Console? If you had an access error, PlayerCCC would not be incremented.
Answer by Zace · Mar 17, 2013 at 02:40 AM
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.
Answer by MrLucid72 · Jan 13, 2016 at 10:30 AM
![alt text][1]
Depending on your version, you may be experiencing a bug: [1]: /storage/temp/61837-array-bug.png
Your answer
Follow this Question
Related Questions
Instantiate JS error that i can't figure out 1 Answer
Bug: cannot assign public Transform variable. Unity 2D 5.2 1 Answer
Keeping a destroyed game object afterwards 0 Answers
make a variable for an array from another script ? 1 Answer
clearing an array and then re-setting it- public variables 0 Answers