- Home /
How to call a function in another script
i have script that adds score on tap of the object, but the score gets deleted after the object does. so i would like to Know how to call a function on another script, in order to update the score, and record the score on a guitext.
here is my code
var Score : int;
function Save() {
if (PlayerPrefs.HasKey("Highscore")) {
if (PlayerPrefs.GetInt("Highscore") < Score) {
PlayerPrefs.SetInt("Highscore", Score);
}
} else {
PlayerPrefs.SetInt("Highscore", Score);
}
PlayerPrefs.Save();
}
function Load() {
if (PlayerPrefs.HasKey("Highscore")) {
Score = PlayerPrefs.GetInt("Highscore");
}
}function Update () {
for(var i:int = 0; i < Input.touches.Length; i++)//How many touches do we have?
{
var touch:Touch = Input.touches[i];//The touch
var ray:Ray = Camera.main.ScreenPointToRay(touch.position);
var hit:RaycastHit = new RaycastHit();
if(Physics.Raycast(ray,hit, 1000))
{
if(hit.collider.gameObject == this.gameObject)
{
switch(touch.phase)
{
case TouchPhase.Began://if the touch begins
DestroyObject(gameObject);
Score++;
break;
}
}
}
}
}
thanks in advance :)
Comment
Answer by 767_2 · Nov 07, 2014 at 03:28 PM
make a gameObject and name it scoreGameObj make a scoreScript and attach it to that and make var name score in it and do the thing below to access it
ThescoreScript = GameObject.Find("scoreGameObj").GetComponent("scoreScript")as scoreScript;
ThescoreScript.score;
Your answer
![](https://koobas.hobune.stream/wayback/20220613170943im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Loading a scene and keeping original score 3 Answers
facebook with unity problem 0 Answers
Update Gui Text with Score 2 Answers