-2

Sorry for asking such a newbie question. Firstly, I searched it on google but I didn't get the results I want. Secondly, I'm pretty new to scripting and unity. Easy question, How to access a static variable from another script? I want to share data between buttons so it won't restart the score when I press it. Any help would be appreciated. Thanks in advanced!

apxcode
  • 7,696
  • 7
  • 30
  • 41
user3854952
  • 25
  • 1
  • 5
  • In essence you can find your answer here: [Static variables in JavaScript](http://stackoverflow.com/questions/1535631/static-variables-in-javascript). Remember UnityScript is basically JavaScript. – apxcode Aug 03 '14 at 23:42
  • @FunctionR I disagree with both of your statements. First, the guy has said he's new to scripting and thread you linked is very much not for newbies. Second, UnityScript is not "basically JavaScript". The code in the answer on the page you linked is not correct UnityScript. – Clonkex Oct 17 '14 at 06:24

1 Answers1

1

Here is an example:

Class name is: test.java

public static int na;

You can access another static by doing this: classname.static

test.na;
Alex L
  • 23
  • 4
  • The file should be called Test.js (capitalized by convention, and .js), but yes, it should work. e.g. Test.na = 2; – Qubei Aug 02 '14 at 11:51
  • actually that worked for accessing other variables, but it didn't share data. Do you know how to share data like static variables but between scripts? Thanks – user3854952 Aug 02 '14 at 13:35
  • It's basically 4 buttons sharing one score GUIText: `var targetScript : ChangeSprite; static var score : int = 0; var guiScore : GUIText; function OnMouseDown () { if(targetScript.spriteRenderer.sprite == targetScript.diamond) { score += 1; guiScore.text = "Score: " + score; } } ` – user3854952 Aug 03 '14 at 00:15
  • The 4 buttons code are different so thats the problem. The other codes are basically the same but `if(targetScript.spriteRenderer.sprite == targetScript.otherthings` – user3854952 Aug 03 '14 at 00:41
  • This is basic stuff... Look up Object Oriented Programming and read a few books or tutorials online. – Savlon Aug 03 '14 at 02:42
  • I said I did search it up but I didn't get the result i want! @Savlon – user3854952 Aug 03 '14 at 02:56