How to access a variable from multiple instances and add them together
Hello.
I have many instances of a game object each with a variable that represents the points scored. When these game objects enter an area, they are added to a list. Once they're on that list, I'd like to be able to access each instance of the gameobject's points variable. And then add them together in a single variable.
For example, if I have 6 instances of the game object in the scene, but I then place 3 instances on the list, I'd like to the points variable on each of those 3 instances to add into a single total score variable. If the points on one instance changes, then I’d like the total score variable to automatically add the new point. If I take one instance off the list, and add a new instance to the list, I'd like the new instance’s points variable to start contributing to the overall score.
Below is where I have got to. Any help you can offer would be greatly appreciated
Script 1: A list to hold instances of the creature game object
public class DungeonVisual : MonoBehaviour
{
public List<GameObject> CreaturesInDungeon = new List<GameObject>();
// plus more code to add and remove creatures to dungeon etc. All that works fine so not copied here
}
Script 2: CreatureManager
CreatureManager script attached to a prefab GameObject called creature. Game starts with creatures instantiating and there are multiple instances of Creatures in the scene. All instances start the scene not on the dungeon list.
Each creature has a variable called creaturePoints. This is a variable is the points acquired by each creature. The creaturePoints is itself derived from a csv file (for test purposes simulating real life point entries) that reads each time a particular instance of the game object gets a new creaturePoints. That part of the script works fine but is not included here. It works by going through the csv file in an IEnumertor method that reads off a new row every second and gets the respective creature’s points for that row.
I've set up an event so that every time a creaturePoints changes it will trigger an event in script 3.
public class CreatureManager : MonoBehaviour
{
private int creaturePoints = 0;
public int CreaturePoints
{
get
{
return creaturePoints;
}
set
{
if (creaturePoints == value) return;
creaturePoints = value;
if (OnPointsChange != null)
OnPointsChange(creaturePoints);
}
}
public delegate void OnPointsChangeDelegate(int newVal);
public event OnPointsChangeDelegate OnPointsChange;
//plus all the other code for the variables and methods associated with creatures
}
Script 3 ScoreManager The aim of this script is so that when creatures are in the dungeon, then the points they acquire while in the dungeon are added to the total score (scoreSum).
My problem is accessing all of the instances of the creature game object in the dungeon list, and adding each creature’s points into a single variable. I'd like it to be so that if just one creature gets a new point then only that point is added to the total.
The bit I’m stuck on #is simply adding each creature’s points to the total when they change in the GetCreatureScore() method.
I think this is because I need to access each instance of CreatureManager and the OnPointsChange variable. But I just can't figure out how to do it. I want it to work automatically when creatures enter the dungeon, but for testing, I'm using a KeyDown method to start it.
I've been working on getting this points and score problem right for literally months.
public class ScoreManager : MonoBehaviour
{
public DungeonVisual dungeonVisual;
public int scoreSum = 0;
public int scoreToDisplay;
public Text scoreText;
void Update()
{
if (Input.GetKeyDown("s"))
if (dungeonVisual.CreaturesInDungeon != null)
{
GetCreatureScore();
}
}
public void GetCreatureScore()
{
GetComponent<CreatureManager>().OnPointsChange += PointsChangeHandler;
}
private void PointsChangeHandler(int newVal)
{
if (dungeonVisual.CreaturesInDungeon != null)
foreach (var creature dungeonVisual.CreaturesInDungeon)
{
scoreDisplay = creature.GetComponent<CreatureManager>().CreaturePointsSum;
scoreSum += scoreDisplay;
scoreText.text = string.Format("{000}", scoreSum);
}
}
}