How to call all instances of a script in a list
I am trying to calculate player score based on the amount of farms (scoreAgent scripts attached to farm prefab) in the game. Each instance will have a different value for the "scoremultiplier" float. i am trying to do a foreach loop in my script but i can get it to add more than one instance of the scoreAgent script. the amount of Score Agents is going to change because you can build more farms as you progress threw the game. so i'm pretty sure i need to use a list and not an array. here is my code. if anyone can fix my code or suggest a better way to do what i explained that would be helpful!
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ScoreTrackerList : MonoBehaviour {
public List<scoreAgent> agents;
public float Score;
public float scoreconstant = 5f;
private void Start()
{
InvokeRepeating("AddScore", 0.0f, 5.0f);
}
void AddScore()
{
agents = new List<scoreAgent>(Resources.FindObjectsOfTypeAll(typeof(scoreAgent)) as scoreAgent[]);
foreach (scoreAgent sA in agents)
{
Score = Score + (scoreconstant * sA.scoreMultiplier);
}
}
private void Update()
{
Debug.Log(agents.Count);
}
}
Comment