Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Scripting' started by Real_BTN4, Oct 20, 2020.

  1. Real_BTN4

    Real_BTN4

    Joined:
    Oct 16, 2020
    Posts:
    23
    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.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerManager : MonoBehaviour
    6. {
    7.     public int coinCount;
    8.     public GameObject FireButton;
    9.  
    10.     public void PickupItem(GameObject obj)
    11.     {
    12.         switch (obj.tag)
    13.         {
    14.             case "Wood":
    15.                 coinCount++;
    16.                 break;
    17.             default:
    18.                 Debug.LogWarning($"WARNING: No handler implemented for tag {obj.tag}.");
    19.                 break;
    20.                 void start()
    21.                 {
    22.  
    23.                     {
    24.                         if (int.currentLevel = 5)
    25.                         {
    26.                             Debug.Log("You have 5 wood!");
    27.                             FireButton.SetActive(true);
    28.                         }
    29.                     }
    30.  
    31.                 }
    32.         }
    33.     }
    34. }
    35.  
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    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.
     
    Vryken likes this.
  3. Real_BTN4

    Real_BTN4

    Joined:
    Oct 16, 2020
    Posts:
    23
    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.
     
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    You haven't declared a "currentLevel" field anywhere, and I'm not sure what you meant to do here:
    Code (CSharp):
    1. if (int.currentLevel = 5)
    "int.currentLevel" is not valid syntax.
     
  5. Sphinks

    Sphinks

    Joined:
    Apr 6, 2019
    Posts:
    267
    I think there went something wrong in the script :). This should work:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class PlayerManager : MonoBehaviour
    5. {
    6.     public int coinCount;
    7.     public GameObject FireButton;
    8.     public void PickupItem(GameObject obj)
    9.     {
    10.         switch (obj.tag)
    11.         {
    12.             case "Wood":
    13.                 coinCount++;
    14.                 break;
    15.             default:
    16.                 Debug.LogWarning($"WARNING: No handler implemented for tag {obj.tag}.");
    17.                 break;              
    18.         }
    19.     }
    20.    
    21.     void Update()
    22.     {      
    23.         if (cointCount == 5)
    24.         {
    25.             Debug.Log("You have 5 wood!");
    26.             FireButton.SetActive(true);
    27.         }        
    28.     }
    29. }
    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
     
    Real_BTN4 likes this.
  6. Real_BTN4

    Real_BTN4

    Joined:
    Oct 16, 2020
    Posts:
    23
    Thank you so much!
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    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?
     
    Sphinks likes this.