Search Unity

OnClick visible counter!?

Discussion in 'Scripting' started by MonaBooo, Feb 13, 2018.

  1. MonaBooo

    MonaBooo

    Joined:
    Feb 13, 2018
    Posts:
    4
    Hi guys,
    I'm very new to Unity and C# and mainly work with tutorials.

    Right now I'm stuck and really need some help.
    What I need to create is some kind of online shopping cart, so I thought the simplest way would be to have a Button that basically adds +1 to a counter.

    I found this script in a tutorial but it simply counts mouseclicks and I want the +1 to be triggered by clicking a button, not just anywhere:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. public class Score : MonoBehaviour {
    5.     public Text scoreText;
    6.     public int score;
    7.     // Use this for initialization
    8.     void Start () {
    9.  
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update () {
    14.         scoreText.text = "Score: " + score;
    15.         if (Input.GetMouseButtonDown(0))
    16.         {
    17.             score++;
    18.         }
    19.     }
    20. }
    I just can't figure out how to edit the script or build and additional onclick function.
    Every idea is much appreciated!
     
  2. LowPoliix

    LowPoliix

    Joined:
    Feb 2, 2018
    Posts:
    3
    Create a public function called OnButtonClick() or whatever you would like. just put score++ inside of it. Then create a UI Button. In the inspector for the button you can see in the Button section there is an On Click(). Create the + sign, find the object that has the script with the OnButtonClick function and drag it in empty box, Click on scroll bar, find your script, find the public function. Bam, everytime you click on the button it will increment the score.

    Code (CSharp):
    1. public void OnButtonClick() {
    2. score++;
    3. }
     
    MonaBooo likes this.
  3. MonaBooo

    MonaBooo

    Joined:
    Feb 13, 2018
    Posts:
    4
    Thank you SO much for the fast reply!!! I'll try that out first thing tomorrow morning!

    Do you by any chance have an idea how to set different counters for different items? Should I make a new function for each item / Button?
     
  4. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Yes. Each Button has its own OnClick in the Unity Editor. When you have a Button in the inspector you follow the directions LowPoliix described, and each Button can call a different function. So each Button can do a different thing.
     
    MonaBooo likes this.
  5. MonaBooo

    MonaBooo

    Joined:
    Feb 13, 2018
    Posts:
    4
    Hey guys, thank you so very much for your help so far!
    As I said, I'm a complete beginner and have basically no idea what I'm doing and I somehow still can't make this work right.

    SO, I used the script I found and deleted the "GetMouseButtonDown" part so it looks like this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Score : MonoBehaviour
    6. {
    7.  
    8.     public Text scoreText;
    9.     public int score;
    10.  
    11.     // Use this for initialization
    12.     void Start()
    13.     {
    14.  
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.  
    21.         scoreText.text = "Score: " + score;
    22.  
    23.     }
    24.  
    25. }
    This script was then assigned to a UI text and the text itself was dragged into the "Script Text" field in the component area.

    Then I created a new script as LowPoliix suggested and called it "ClickToAdd":

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ClickToAdd : MonoBehaviour
    7. {
    8.  
    9.     public void AddOne()
    10.     {
    11.        
    12.             score++;
    13.     }
    14.  
    15.  
    16. }
    This script was assigned to a seperate GameObject and then I chose "AddOne" as an onclick function for my UI button.

    When I now tried to run the game an error occured saying "score" couldn't be found in the document (referring to the word "score" in the ClickToAdd script)

    After a little trial and error the best I could come up with on my own was
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ClickToAdd : MonoBehaviour
    7. {
    8.  
    9.     public void AddOne(int score)
    10.     {
    11.        
    12.             score++;
    13.     }
    14.  
    15.  
    16. }
    Now I can run the game, see the score text and can actually click the button - it just doesn't do anything.

    Again, I'm so thankful for every idea how to fix this.

    Even a completely different way of achieving the result of having a counter for button clicks would be fine!! Or just a simple counter with a "+" and "-" button or something.

    Thanks again!!!!
     
  6. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Your score variable is on the Score script. Its the only one that sees it. You need to add references to it so you can access that variable. Additionally if we add a reference to Score script we can make a new method called UpdateScore, that only updates the score text when we need to. Not every single Update() as your code currently does. Try something like this:

    Add this to the Score Script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. public class Score : MonoBehaviour
    5. {
    6.     public Text scoreText;
    7.     public int score;
    8.     // Use this for initialization
    9.     void Start()
    10.     {
    11.     }
    12.     // Update is called once per frame
    13.     void Update()
    14.     {
    15.             // Don't Update Score here
    16.     }
    17.    //  Update function other scripts can all
    18.    public void UpdateScore(int adjust)
    19.    {
    20.          score+=adjust;
    21.          scoreText.text = "Score: " + score.ToString();
    22.    }
    23. }
    Then change any code that needs to adjust the score to something like this:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. public class ClickToAdd : MonoBehaviour
    6. {
    7.     // This is a link to your score script
    8.     // You need to drag the actual Score script onto this in the editor
    9.     public Score scoreScript;
    10.  
    11.     public void AddOne()
    12.     {
    13.             scoreScript.UpdateScore(1);
    14.     }
    15. }
    public Score scoreScript will create a spot in the editor for you to drag something onto. Just drag your GameObject that has Score script on it onto this spot (NOT the score script sitting in your assets ).
     
    MonaBooo likes this.
  7. MonaBooo

    MonaBooo

    Joined:
    Feb 13, 2018
    Posts:
    4
    IT WORKED!!!

    Thank you all so very much, I'd been lost without you!!!
    I'm so pumped right now, finally I can finish this project.

    Thanks for helping this noob!!!