- Home /
My static variable is not changing
I'm just trying a simple test to get my static variable to change. I believe static variables are global variables and should be changeable from other scripts but it's not working out for me.
In one object I have a script that sets up the variable.
static var score: int = 0;
And in a different object and script I try to reference it.
var SCORE :int = GetComponent(S_Player).score;
function OnTriggerEnter(other: Collider){
if (other.gameObject.tag == "Fish"){
other.transform.position.y = 8;
other.transform.position.x = Random.Range(-7,7);
//check if asset is there...
if (P_Death){
Instantiate(P_Death,transform.position,transform.rotation);
SCORE+=1;
Debug.Log("Works");
}
Destroy(gameObject); }
}
Sorry the code looks crummy the blockcode doesn't seem to like formatting. Anyway, when I hit an object I should add 1 to my score. Well, when I run the function and I hit the object everything works but the adding the score part. I don't get any errors, and my score doesn't change. My debug log works so my function is working. So I'm guessing this is a logic problem so what did I do wrong? Is it just poor referencing and if so what is a better method? So yes, my whole function works, I have no errors messages but I'm not adding to my score.
Answer by mpavlinsky · Mar 12, 2012 at 09:43 PM
When you set an integer equal to another integer like that it does a copy by value so your integer score is equivalent to SCORE when you initialize it, but they point to different locations in memory so changes in one won't be seen in the other. If you want to change a simple static int like that and have your changes get reflected everywhere you either need to change that specific variable (S_Player.score) or you'll need to wrap it in a class.
It looks as though you are attempting to use the variable as a public and not a static though. You could access your static variable by simply using S_Player.score, you don't need to find the specific component of that class you have since a static variable is shared across all instances of that class. Your code the way it is written now would still compile and run if you made the variable in question public instead of static, of course it would have the same outcome as before because of the issues I outlined in the first paragraph.
Answer by Quiet · Mar 13, 2012 at 12:15 AM
So my GetComponent(S_Player).score; is pretty useless then? Awe. I thought it allowed me to change the variable, but it basically just gives me a copy right?
I don't understand exactly what you said, what would be the right class to use if I want to change that specific variable through different scripts?
Could I just make my variable a public static variable so there is only one variable used by all instances?
Firstly, please comment on my answer rather than post another answer, and please remember to mark my answer as accepted if it's sufficient.
You are exactly right that you want a public static variable. Then you would just lose the GetComponent call and access and modify it via S_Player.score.
As for the class thing, I'm not sure how this works because I work exclusively in C# but there are basically only references to classes in C#. So when set a class variable equal to another one they just point at the same data rather than having a copy of the same data. I'm not sure this is the most efficient way to do this, nor would it be my suggestion but just to show what I mean (sorry for the formatting, it looks good in the text field but I guess your not supposed to put code in comments):
public class Wrapper { public int data = 0; }
int data1 = 0;
int data2 = data1; data2 = 1; Debug.Log( data1 + " | " + data2 ); // 0 | 1
Wrapper wrapper1 = new Wrapper(); Wrapper wrapper2 = wrapper1; wrapper2.data = 1; Debug.Log( wrapper1.data + " | " + wrapper2.data ); // 1 | 1
Sorry for the bad formatting, but it's UnityAnswers fault. Everything looks good when I'm editing.
Sorry, I'm new to this site and didn't realize I posted an answer ins$$anonymous$$d of a comment. Sorry about that. I have not done much with C# so that doesn't much, but it seems like this should not be this complicated, so I'll look more into it later. Thank you for your help though.
Your answer
Follow this Question
Related Questions
Global Variables Refuse to Cooperate 1 Answer
Not asigned health script 1 Answer
Changing static variables from another script? 1 Answer
Static variable issue in published 1 Answer
Global variables help 1 Answer