Y
Y
Yuri2020-07-14 12:44:03
C++ / C#
Yuri, 2020-07-14 12:44:03

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;}

 
 }

And here is the Player script:
public class Coin
{
int CoinsCount;
public  void OnTriggerEnter2D(Collider2D col) 
{
  if(col.gameObject.CompareTag("Coin"))
  {
    CoinsCount++;
    Destroy(col.gameObject);
  }
 }
}

Can you please tell me how to implement this correctly. I will be grateful.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
fan92rus, 2020-07-14
@uriy99

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 question

Ask a Question

731 491 924 answers to any question