0

I have simple coin counter, in 2d Unity game. But It's count one coin as 2. I think that it's because of wrong converting int to string.

public GameObject coin; // Gameobject with coin
public Text CoinCounter; // Text with counter that shows in game
private float TotalCounter = 0; // Float for counting total amount of picked up coins

{
   TotalCounter = Convert.ToInt32((CoinCounter.text)); // Converting text counter to Numbers
}

private void Update()
{

    TotalCounter = Convert.ToInt32((CoinCounter.text)); // Updating Counter evry frame update 
    Debug.Log(TotalCounter); // Showing Counter  in Console 
}

private void OnTriggerEnter2D(Collider2D collision)
{
    TotalCounter = (TotalCounter + 1); // adding 1 to total amount when player touching coin 
    CoinCounter.text = TotalCounter.ToString(); // Converting to Text, and showing up in UI





    coin.SetActive(false); // Hiding coin


}

So, in Debug Log it's showing right Total amount, but in UI, It's Showing wrong number. As example, When Total Amount is 1 it's showing 2 etc.

  • I dont understand this part: `{ TotalCounter = Convert.ToInt32((CoinCounter.text)); // Converting text counter to Numbers }` can you please explain what that is? a method? a setter? – iSpain17 Feb 22 '19 at 10:09
  • 1
    Why is TotalCounter declared as a float and not an integer? It also looks like you have a missing method declaration in the code above. – Andrew Feb 22 '19 at 10:10
  • is there any value update event occurs in UI level? like when the data will come from the this blocks, do you update them again with +1 – Md. Tazbir Ur Rahman Bhuiyan Feb 22 '19 at 10:14
  • Why do you need to read TotalCounter from text box in update, i dont think that is necessary here, just update text and total counter whenever you hit a coin – Anil Feb 22 '19 at 10:22
  • @Andrew > Why is TotalCounter declared as a float and not an integer? Float and int can hold same types of value (int can't hold decimal numbers, I know, but float can hold regular numbers), didn't they? > It also looks like you have a missing method declaration in the code above. Whant method it's not declarated? – Emerick Grimm Feb 22 '19 at 10:31

3 Answers3

0

You don't have to do it in Update but only when you actually change it.

The method you are looking for is probably int.TryParse

you should use int for amounts (unless will you have values like 1.5 coins later)

Than you execute your code everytime it collides ... with anything. You should only collide with a coin instead. Either use tags or compare with the referenced value in your case

public GameObject Coin; // Gameobject with coin
public Text CoinCounter; // Text with counter that shows in game
private int _totalCounter = 0; // Int for counting total amount of picked up coins

// I guess it's supposed to be Start here
void Start()
{
   // Converting text counter to Numbers
   int.TryParse(CoinCounter.text, out _totalCounter); 
}

private void OnTriggerEnter2D(Collider2D collision)
{
    if(collision.gameObject != Coin) return;

    // later this should probably rather be
    //if(collision.gameObject.tag != "Coin") return

    _totalCounter += 1; // adding 1 to total amount when player touching coin 
    CoinCounter.text = _totalCounter.ToString(); // Converting to Text, and showing up in UI

    Coin.SetActive(false); // Hiding coin

    // later this should probably rather be
    //collision.gameObject.SetActive(false);
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
0

It's better to write your convertor code into the trigger void , after that check it ; this might be happened because of update function, try like that and check again: `

public GameObject coin;
public Text CoinCounter;
private float TotalCounter = 0; 
private void Update()
{}
private void OnTriggerEnter2D(Collider2D collision)
{
    TotalCounter = (TotalCounter + 1); 
    Debug.Log(TotalCounter); 
    CoinCounter.text = TotalCounter.ToString(); 
    Debug.Log(CoinCounter.text);
    coin.SetActive(false); 
}

`

0

Problem was not in converting, trigger worked twice. Need check if the coin is enabled before disabling it and adding to the coin counter. Eg:

 if (coin.activeSelf)
  {
      coin.SetActive(false);

      Debug.Log("Object is not active ");

      TotalCounter += 1;
      Debug.Log("Total Counter + :" + TotalCounter);
      CoinCounter.text = TotalCounter.ToString();
      Debug.Log("Text after +:" + CoinCounter.text);
  }