I have two scripts, GameMaster and ItemCollector.
**GameMaster script:**
public class GameMaster : MonoBehaviour
{
private static GameMaster instance;
[SerializeField] public int maxCherries;
[SerializeField] public bool[] cherriesCollected = new bool[100];
public int numberOfCherriesCollected;
public GameObject[] cherries = new GameObject[100];
private void Start() {
cherries = GameObject.FindGameObjectsWithTag("Cherry");
}
private void Awake() {
Debug.Log(instance);
if(instance == null){
instance = this;
DontDestroyOnLoad(instance);
}
else{
Destroy(gameObject);
}
Debug.Log(numberOfCherriesCollected);
}
}
**ItemCollector script:**
public class ItemCollector : MonoBehaviour
{
private GameMaster gameMaster;
private void Start() {
gameMaster = GameObject.FindGameObjectWithTag("Game Master").GetComponent();
Debug.Log(gameMaster.numberOfCherriesCollected);
}
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.gameObject.CompareTag("Cherry"))
{
int cherryId = collision.GetComponent().id;
gameMaster.cherriesCollected[cherryId] = true;
gameMaster.numberOfCherriesCollected++;
Debug.Log("collected cherry at: " + cherryId);
for(int i = 0;i
↧