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

Question Need help for 2 script

Discussion in 'Scripting' started by Arabman, May 3, 2023.

  1. Arabman

    Arabman

    Joined:
    May 3, 2023
    Posts:
    2
    First script :
    i use this script to decrease a counter and reset it when it reach maxvalue


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

    public class NumberTextDecreaser : MonoBehaviour
    {
    public int maxValue;
    public TextMeshProUGUI numberText;
    int counter;

    public void ButoonPressed()
    {
    if (counter <= maxValue) counter = 31;
    counter--;
    numberText.text = counter + "";

    }




    }

    what i need is : that a second button decrease a number between counter and maxvalue. The counter is decreased by a first on button click.



    Second script :

    A textmeshpro with is increased by a onclick button, and a second onclick button that decrease a counter.
    when the counter reach maxvalue, the textmeshpro reset to 0.

    I hope i was clear and thanks for the answers.,
     
  2. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
  3. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    This is from Chat GPT - maybe you could consider use it if you want ask question like this

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class NumberTextDecreaser : MonoBehaviour
    7. {
    8.     public int maxValue;
    9.     public TextMeshProUGUI numberText;
    10.     int counter;
    11.  
    12.     public void DecreaseButtonPressed()
    13.     {
    14.         if (counter > 0) {
    15.             counter--;
    16.             numberText.text = counter.ToString();
    17.         }
    18.     }
    19.  
    20.     public void ResetButtonPressed()
    21.     {
    22.         counter = 0;
    23.         numberText.text = counter.ToString();
    24.     }
    25. }
    26.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class TextMeshProCounter : MonoBehaviour
    7. {
    8.     public int maxValue;
    9.     public TextMeshProUGUI textMesh;
    10.     int counter = 0;
    11.  
    12.     public void IncreaseButtonPressed()
    13.     {
    14.         counter++;
    15.         if (counter > maxValue) {
    16.             counter = 0;
    17.         }
    18.         textMesh.text = counter.ToString();
    19.     }
    20.  
    21.     public void DecreaseButtonPressed()
    22.     {
    23.         if (counter > 0) {
    24.             counter--;
    25.             textMesh.text = counter.ToString();
    26.         }
    27.     }
    28. }
    29.  
     
  4. Arabman

    Arabman

    Joined:
    May 3, 2023
    Posts:
    2
    Thanks for the answer. Yes, Chat GPT didnt come to me in the mind.