Adding points from another script
I have to scripts, one is on a InputField.
void CompareGuesses(string guess){
if(guess.ToLower() == gameName.ToLower()){
text.text = "You Guessed Correctly";
SceneManager.LoadScene("Level 1");
} else if(guess != gameName){
text.text = "Wrong!";
} else if(guess != gameName){
text.text = "Wrong!";
}
}
This is the script if they answer correctly, and I need it to add 1 point to my score on this script.
using UnityEngine; using System.Collections; using UnityEngine.SceneManagement;
public class GameStatus : MonoBehaviour {
public int score = 0;
static GameStatus instance;
public static GameStatus GetInstance(){
return instance;
}
void Start () {
score = PlayerPrefs.GetInt ("score", 0);
}
void Update () {
}
void OnDestroy(){
Debug.Log ("Fail");
PlayerPrefs.SetInt ("score", score);
}
public void AddScore(int s){
score += s;
}
public void LoadScene(string sceneName){
SceneManager.LoadScene (sceneName);
}
}
Answer by TrueMaskss786 · Aug 06, 2016 at 05:58 PM
You can just call the function AddScore() when the answer is correct and after that you can update the score variable.
But Before that you should get the reference of that script
ScoreScript object;
object=GetComponent<ScoreScript>();
if(guess.ToLower() == gameName.ToLower()){ text.text = "You Guessed Correctly";
here you can call AddScore and pass value that you want to increment
object.Addscore(1);
And in add score function just update the score
public void AddScore(int s)
{
score += s;
PlayerPrefs.SetInt("score",score);
}
Note that both the script should be on same gameobject
Your answer
Follow this Question
Related Questions
Making several right answers 1 Answer
Making an image appear 0 Answers
How can i disable/enable slowly the blur effect using the blur script ? 0 Answers
How can i create/spawn some objects and set random angle for each one ? 0 Answers
How can I give each instance a enum mode from another script ? 0 Answers