using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreManager : MonoBehaviour
{
public static int scoreCount;
public static int highScoreCount;
public Text scoreText;
public Text highScoreText;
public GameObject zombie;
public CanvasGroup score;
// Start is called before the first frame update
void Start()
{
scoreCount = 0; // set the score to 0 at the start of the game
highScoreCount = PlayerPrefs.GetInt("HighScore", highScoreCount); // store the highScore
highScoreText.text = "High Score: " + highScoreCount.ToString(); // display the player's high score
score.alpha = 1;
score.interactable = true;
score.blocksRaycasts = true;
}
// Update is called once per frame
void Update()
{
if (GameObject.FindGameObjectWithTag(tag: "Zombie") == null)
{
scoreCount += 100;
scoreText.text = "Score: " + scoreCount.ToString(); // display the player's score
if (scoreCount > highScoreCount)
{
highScoreCount = scoreCount;
PlayerPrefs.SetInt("HighScore", highScoreCount);
PlayerPrefs.Save();
}
if (Time.timeScale == 0)
{
score.alpha = 0;
score.interactable = false;
score.blocksRaycasts = false;
}
}
}
}
This code is working great for keeping the player’s score, but each kill it keeps adding 100 points. Not stopping until another zombie spawns. think line 28 could be the culprit, but if it is I don’t know the fix.
Don’t run your score incrementing code in Update. That runs every frame. Instead, run it when the zombie dies. You could have the Zombie script call a function on your ScoreManager to increment the score when it dies.
No, literally every Unity project would break if GetComponent was bugged. Show us the code that doesn’t work. Most likely, you’re attempting to call GetComponent on an object that doesn’t have the component being requested.
You’re right, I don’t think that’s even a possibility, I’m probably just messing up somewhere…
As for the code that doesn’t work. here it is:
You won’t be able to use that variable in any other functions. You need to declare ScoreManager scoreManager; OUTSIDE of any functions to make it into an instance variable which can be used across different functions.
Not really but that may work too since scoreis an instance variable. Does that object also have the ScoreManager attached to it? It will work if it does.
public GameObject score;
private ScoreManager scoreManager ; // Global declaration of your object instance
private void Awake()
{
scoreManager = score.GetComponent<ScoreManager>();
}
With that global declaration you can use your object instance in the whole class. If you declare it in a method it´s only “visible” inside of that method, means you can only use it inside that method
Where is your scoreManager script located? You can create a new empty gameobject called"Game Manager" and put scoremanager in it so you can easly findd it from your zombie code. Put a tag on your gamemanager.
The way I did it score runs under its own Panel called Score in a Canvas called UI. I would think the problem is having the script located in a canvas except I am unable to use GetCompontent on any script in my game.
You can add an empty gamobject as child of your canvas and add the script to that gameobject. Set a tag like “ScoreManager” (or anything else) o the gameobject and it should work like @Atix07 said (you just have to adapt the tag name in the method FindGameObjectWithTag).
My only problem is that It’s the prefabbed zombie is what’s trying to access the ScoreManager. Should I prefab the empty object that has the script on it? Or take another approach?
But why is that a problem ? if you access the empty gameobject with the script to your canvas and put a tag on it, you can access it from where ever you want. You just need any gameobject that has the script (and the tag).
No i wouldn´t create a prefab of the that empty gameobject only for that.