Answer the question
In order to leave comments, you need to log in
How to implement the SingleTon pattern for collecting coins?
In the PlayerInventory script, implement (template) Singleton.Make the accrual of coins by referring to the Instance of this class. The collection of coins is implemented in the Player script.
Below is my code:
public int CoinsCount;
private void Awake()
{
Debug.Log(Player.Instance.CoinsCount);
}
public static Player Instance {get;set;}
}
public class Coin
{
int CoinsCount;
public void OnTriggerEnter2D(Collider2D col)
{
if(col.gameObject.CompareTag("Coin"))
{
CoinsCount++;
Destroy(col.gameObject);
}
}
}
Answer the question
In order to leave comments, you need to log in
You can use Di container - here are configs https://habr.com/ru/post/302240/
For autofac instaneType config https://autofaccn.readthedocs.io/en/latest/lifetim...
you can use another container everywhere.
you create, you add an inventory to the container as a Singleton
,
you inject
it into the Player object and call the addition of coins.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question