- Home /
calling functions help {javascript}
in the following script, on the touch of the object it is supposed to call a function from another script, the script is scoring, and the function is addscore. any help would be appreciated
var go = GameObject.Find("scoreGameObj");
var Score : int;
function Start (){
Score = 0;
}
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
Debug.Log("you have tapped one");
go.GetComponent(scoring).addscore ();
DestroyObject(gameObject);
Score++;
break;
}
}
}
}
}
thanks in advance
Answer by zharik86 · Nov 11, 2014 at 08:04 AM
If touch works correct, that problem in your function addScore(). Try transfer parametr Score:
...
case TouchPhase.Began:
Score++;
go.GetComponent(scoring).addscore(Score);
...
And change function in second script
function addscore(var tpScore: int) {
Counter = tpScore;
guiText.text = ""+Counter;
if (Counter > 100) {
guiText.anchor = TextAnchor.UpperRight;
guiText.pixelOffset = Vector2 (100,351);
}
}
I hope that it will help you.
it has an error "unexpected token : tpScore", though everything is like it is ^ |
Sorry, it's write as CSharp, but you use Java. I update my answer.
@Hex2013 if I help you, please, mark my answer (below vote button).
Your answer
Follow this Question
Related Questions
C# - How do I increase the variable from another script? 2 Answers
OnGUI is not being called 1 Answer
Game responds slow to device event 0 Answers
Setting 2 variables from different classes equal to each other. 1 Answer
How can I use something like Invoke() to call a method in another class? 1 Answer