- Home /
Question by
GreenTee · Jul 27, 2014 at 05:31 PM ·
javascriptgamescorecounter
Connecting 2 scripts(javascpt)?
Hello,
i want to ask how to link two scripts because i want to make a counter that is shown in a score box. I created a var "ScoreCounter" and a seperate script for my Score but i can't import the var form one script to the other
script for the GUI
#pragma strict
var Pos1 : float = 400;
var Pos2 : float = 15;
var Pos3 : float = 155;
var Pos4 : float = 25;
function Update () {
}
function OnGUI () {
// Make a background box
GUI.Box (Rect (Pos1,Pos2,Pos3,Pos4), "Score: " + ScoreCounter);
}
script for the counter
#pragma strict
static var ScoreCounter : int = 0;
var touchCoin : boolean = false;
function Update () {
if(touchCoin == true)
{
Destroy(gameObject);
ScoreCounter += 1;
Debug.Log("Destroy Coin and add 1");
}
}
function OnCollisionEnter2D (coll: Collision2D)
{
if(coll.collider.tag == "Player")
{
touchCoin = true;
Debug.Log("Coin is touching");
}
}
I hope somebody is able to help me :)
Greetings GreenTee
Comment
Best Answer
Answer by tanoshimi · Jul 27, 2014 at 05:54 PM
Seeing as ScoreCounter is static:
GUI.Box (Rect (Pos1,Pos2,Pos3,Pos4), "Score: " + ClassInWhichScoreCounterIsDefined.ScoreCounter);
Answer by GlorifiedPig · Jul 27, 2014 at 06:25 PM
#pragma strict
static var ScoreCounter : int = 0;
var touchCoin : boolean = false;
var Pos1 : float = 400;
var Pos2 : float = 15;
var Pos3 : float = 155;
var Pos4 : float = 25;
function Update () {
if(touchCoin == true)
{
Destroy(gameObject);
ScoreCounter += 1;
Debug.Log("Destroy Coin and add 1");
}
}
function OnCollisionEnter2D (coll: Collision2D)
{
if(coll.collider.tag == "Player")
{
touchCoin = true;
Debug.Log("Coin is touching");
}
}
function OnGUI () {
// Make a background box
GUI.Box (Rect (Pos1,Pos2,Pos3,Pos4), "Score: " + ScoreCounter);
}
Use that code in 1 script.
its also good, but i want to seperate the scripts, because i think a counter sholdn't be added to a coin :p