I'm making an FPS which has a "Player" class attached to a game object in a scene. This class keeps track of things like player health, ammo etc. and has functions for various player behavior.
I need to access this data frequently from other instantiated classes - a good example would be medkits spread across the levels. The way things are currently set up, the Player class has a SetPlayerHealth function which modifies the health of the player, and each pickup detects if collided with and calls this function in the instantiated Player class, passing along the amount of health to modify by.
This works (and I like having pickups and such detect the collision in their own class since it's more modular), but I'm struggling with figuring out the best way to actually refer to the instanced Player class.
I can make a "player" variable in the other classes and do this either when spawned or when needing to call the Player class:
player = GameObject.FindGameObjectWithTag("Player").GetComponent();
And then refer to this variable. But while this works well for objects there are just a few, set amounts of (say, UI elements) it seems badly optimized for things like pickups/medkits which continuously instantiate and there are many of - to my understanding finding game objects by tag at runtime isn't great for performance.
What would be the optimal/recommended way to refer to the instantiated Player class efficiently? (or is there a better approach to basic player structure altogether?)
↧