Hi,
simply, how can control a variable like next “score” from any javasript files …?
var score : float = 00000;
function Update () {
score += 20.0;
guiText.text = "Score " + parseInt(score).ToString("#,0");
}
Hi,
simply, how can control a variable like next “score” from any javasript files …?
var score : float = 00000;
function Update () {
score += 20.0;
guiText.text = "Score " + parseInt(score).ToString("#,0");
}
Hello,
you can “find” the script object and its variables using Find(), but I suggest you to include, in the required objects a reference to the object containing the script self.
Take a look to:
Transform.Find()
GameObject.Find()
Cheers!
can explain more…!
if i want control a variable in object, this object must be child of other object…?
Generally speaking, you can use javascripts in two ways:
Create a script, then put it inside an object (we can say you are creating a “script instance”: the term is not very correct, but it is just to explain the basic idea).
Create a script, and use it like a “singleton”: it means you can use the script (and variables inside) like a static object (Object you can use everywhere without creating an instance, like a “global script”).
Singleton could be a good solution: in fact you can create a javascript script containing a class which manages the score for the game.
For singleton, look at the following topic:
http://forum.unity3d.com/viewtopic.php?t=19212&highlight=javascript+singleton
If you still have problems (after reading post
) please write me again, so I can try to write a bit of code just to clear your questions.
Cheers!
I’m using way 02, creating object then assign to it js code, for public score class I wrote that:
public static class MySingleton{
var score : float = 0.0;
function Update () {
guiText.text = "Score " + parseInt(score).ToString("#,0");
}
}
still gave me errors, it told me Unidentified variable
Sorry, but in which programming language are you writing?! It seems Java, but Unity works in Javascript (which is VERY different from java), or you can use C#.
javascript
oK, sorry, my mistake.
Probably the error is in “guiText” assignement. That “object” must be referenced with a “pointer”.
Try to find it using Find() or GameObejct.Find().
Cheers
I added Find() but the score still count automatically, i want score increases only when ball collide red box collider, this my full files, can you tell me where is my mistake
239751–8576–$ball_305.rar (859 KB)
Ok, now I’m in office, and I will check your files later (please wait for about 5 hours, sorry!). I need to go home, and I will check it in the night (I work in Unity for fun, so my free time is only in the night, and my best friend is the coffee!
)
Cheers!