the way my game works is that when the player enters the scene the enemies are spawned in. So i wanted to create a system that tracks player kills, and although this would be easy on its own i want to design it in a way that it passes through scenes and appears on a separate script.
my initial thought was to add 1 to a static variable every time ai.health = 0 however because enemies are spawned in am not sure how to make that static variable accessible on another script on another game object.
If you find this confusing i would be happy to further explain.
any feedback or help would be greatly appreciated.
a few things. Do you already have a place that updates the kills, if so you’ll want to reference it. If not then do everything in a second script (completely new).
IE
public class KillsManager : Monobehaviour
{
public static KillsManager kills;
public int killcount = 0;
//this makes sure that it exists and that only one exists at a time
public void Awake()
{
if(kills == null)
{
kills = this;
}
else if(kills != this)
{
Destroy(gameObject)
}
//THIS IS THE PART THAT MAKES SURE THE INFORMATION PERSISTS BETWEEN SCENES
DontDestroyOnLoad(gameObject)
}
public void Update()
{
//this will cause it to change everytime you get a kill
killcount = UIManager.uimanager.score (or whatever goes up by 1 when you get a kill)
//script.staticName.variable
}
}
so now your kill count will follow you everywhere you go. anytime you go back into the game tho, your kill count will reset to 0 since the KillManager only reads what the player Kills are. So if you want that to override you’ll need to make a method inside where ever you change the kill amount where playerkills = KillManager.kill.killcount
You want the counter inside your player? you access that var using the class_name.var_name
if the class that contains the public static int numberOfKills is Player it would be:
Player.numberOfKills