- Home /
How to change an int in a different class (for score)
So what i'm trying to achieve is that everytime a zombies health reaches 0 the players score is increased by one. I currently have no clue why this sin't working. Here is my code
Code within the enemy class:
if (health <= 1) {
SurvivalScore SS = (SurvivalScore)target.GetComponent ("killscore");
SS.killscore (+1);
}
Code within the scoring system:
public int zombieskilled
public void killscore(int adj) {
zombieskilled += adj;
}
Any help will be appreciated as currently the score is always displayed at 0.
Answer by Burla · May 08, 2014 at 05:38 PM
Assuming that SurvivalScore is the name of the scoring system script, then you could do it this way.
First declare the variable at the top of the script in the enemy class.
SurvivalScore SS;
Then you would make the reference in the Start function like this.
void Start() {
SS = gameObject.GetComponent<SurvivalScore>();
}
Now you're able to call the killscore function via the reference you just made like this.
if (health <= 1) {
SS.killscore(1);
}
Let me know if this helped you.
Unfortunately this didn't help. But i ended up figuring it out. Here is how i did it. GameObject $$anonymous$$ainCamera = GameObject.Find ("$$anonymous$$ainCamera"); SurvivalScore SS = (SurvivalScore)$$anonymous$$ainCamera.GetComponent(typeof(SurvivalScore)); SS.killscore (+1);
Your answer
![](https://koobas.hobune.stream/wayback/20220613144549im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Count the Score 1 Answer
Adding coin score to multiplied value 2 Answers
How to make an AI like slender ? 1 Answer
Error: "ArgumentException: get_deltaTime can only be called from the main thread" 1 Answer
Coins and Enemies 3d 1 Answer