- Home /
Calling script functions from other objects doesn't work on mobile but it does in the editor
Just to be clear, everything works perfectly in the editor and I have no problems, so I don't know what I can change.
This is the code in one script
if(Input.GetKeyDown(KeyCode.Space) && canDoTrick){
if(trickTime >= 1){
scoreScript.SendMessage("addScore", points);
}
}
this is the code from the script i'm trying to call
public void addScore(int num){
score += num;
changeDigits(toArray(score));
}
I actually didn't use sendmessage before, before I had a variable of the script and I called it by assigning it in the inspector, but I tried switching it to sendmessage to see if that would work on mobile, but it didn't.
Any ideas? I've really hit a wall here I don't know what I can change because it works fine in the editor.
Answer by Taorcb · Apr 29, 2016 at 04:03 AM
Try just calling scoreScript.addScore(points)
. I see no reason why either solution shouldn't work.
Answer by Jason2014 · Apr 29, 2016 at 08:11 AM
Make a reference to object with this script attached and get component from it. Then using a variable (this reference) get access to this function.
public ScoreScript scoreScript;
void Awake ()
{
scoreScript = GameObject.FindGameObjectWithTag ("your tag").GetComponent <ScoreScript> ();
}
[...]
if (Input.GetKeyDown (KeyCode.Space) && canDoTrick)
{
if (trickTime >= 1)
scoreScript.addScore (points);
}
Best way (at least for me) to access to game objects and then to components is by defined tag. Of course this is one of ways, you can access to it different, but SendMessage isn't a good option.
Your answer
Follow this Question
Related Questions
Joystick trouble... 0 Answers
c# or java for mobile version 2 Answers
Copy mp3 to cellphone 0 Answers
UI works in editor, but not on mobile device 1 Answer
movie texture in mobile devices 3 Answers