How do I detect how many coins that are in my int count?

So I’m having trouble with this script I am working on. I want it to detect when I get to five coins and setactive my button.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerManager : MonoBehaviour
{
    public int coinCount;
    public GameObject FireButton;

    public void PickupItem(GameObject obj)
    {
        switch (obj.tag)
        {
            case "Wood":
                coinCount++;
                break;
            default:
                Debug.LogWarning($"WARNING: No handler implemented for tag {obj.tag}.");
                break;
                void start()
                {

                    {
                        if (int.currentLevel = 5)
                        {
                            Debug.Log("You have 5 wood!");
                            FireButton.SetActive(true);
                        }
                    }

                }
        }
    }
}

First, your void start appears to be inside your PickupItem method. Now, I know you can put methods in methods, but they behave a bit different. ( I actually haven’t messed with putting a method in a method yet…)

Also, if you actually wanted Unity to run the “start” method, it has to start with a capital letter.

void Start()

Next, you are incrementing coinCount when you collect “Wood”, so you simply need to check if coinCount == 5.

Also, your if statement is incorrect. When doing a comparison, you want to use a == to compare. A single = is for assignment.

1 Like

I don’t think currentLevel works for ints. Do you know what I could put there instead? I am trying to make it so that when I get 5 wood I can press a button.

You haven’t declared a “currentLevel” field anywhere, and I’m not sure what you meant to do here:

if (int.currentLevel = 5)

“int.currentLevel” is not valid syntax.

I think there went something wrong in the script :). This should work:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerManager : MonoBehaviour
{
    public int coinCount;
    public GameObject FireButton;
    public void PickupItem(GameObject obj)
    {
        switch (obj.tag)
        {
            case "Wood":
                coinCount++;
                break;
            default:
                Debug.LogWarning($"WARNING: No handler implemented for tag {obj.tag}.");
                break;              
        }
    }
   
    void Update()
    {       
        if (cointCount == 5)
        {
            Debug.Log("You have 5 wood!");
            FireButton.SetActive(true);
        }        
    }
}

Maybe the naming should be changed a little bit. It´s confusing, if you increase a variable with the name “cointCount” if actaully wood was collected

1 Like

Thank you so much!

Ah, no. Don’t do the check in Update. Just check it after you increment coinCount.
The issue here is once coinCount == 5, you now are running a debug message every frame which can create slowdowns. No reason to do this in Update since you don’t need to check this every frame. While removing the debug is a good idea, again, why check for something you don’t need to every frame?

1 Like