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 know if the user clicked on specific button

Discussion in 'Scripting' started by fa4da, Mar 12, 2021.

  1. fa4da

    fa4da

    Joined:
    Feb 3, 2021
    Posts:
    15
    hello

    so i have a game and its contains question with 3 options(buttons), i want my code as the following: if the user clicked on the correct answer(button) i will give him scene that's contain specific feedback, and the same for the other answers.
    i've been trying many solutions and nothing worked :(
     
  2. PutridEx

    PutridEx

    Joined:
    Feb 3, 2021
    Posts:
    1,120
    fa4da likes this.
  3. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    Simplify the behavior you want. In this case, when the user clicks a button it sends a managing script which button it was. That's all. It does not concern itself whether the answer is correct or not, your managing script does that and reacts accordingly. So each button should call a method in the managing script and pass a value indicating which button it was (like 1, 2, 3). Use the same method for each, just change the value passed, which you can set in the Inspector Event ui.
     
    fa4da likes this.
  4. Havyx

    Havyx

    Joined:
    Oct 20, 2020
    Posts:
    140
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.SceneManagement;
    4.  
    5.  
    6. public button button01;
    7. public button button02;
    8. public button button03;
    9.  
    10. void Start()
    11. {
    12.     button01.onClick.AddListener(delegate { LoadScene(1); } );
    13.     button02.onClick.AddListener(delegate { LoadScene(2); } );
    14.     button03.onClick.AddListener(delegate { LoadScene(3); } );
    15. }
    16.  
    17. private void LoadScene(int sceneToLoad)
    18. {
    19.     SceneManager.LoadScene(sceneToLoad, LoadSceneMode.Single);
    20. }
    21.  
    22.  
     
    fa4da likes this.
  5. fa4da

    fa4da

    Joined:
    Feb 3, 2021
    Posts:
    15
    thank you all you helped me a lot:)