Juan Don Guterez
Juan Don Guterez

Reputation: 37

Check if button is clicked

I have a level selection screen with buttons. I have a script for that and a Level Control script. The buttons are in LevelSelection script. How can i in LevelControl script check what button is last clicked from LevelSelection script. Here is the LevelSelection script for buttons:

public Button _lvl1_1;
public Button _lvl1_2;
.
.
.

 void OnEnable()
    {
        
        Button lvl1_1 = _lvl1_1.GetComponent<Button>();
        Button lvl1_2 = _lvl1_2.GetComponent<Button>();

        lvl1_1.onClick.AddListener(Level11);
        lvl1_2.onClick.AddListener(Level12);

        cam = Camera.main;
        coins.text = PlayerPrefs.GetInt("Coins", 0).ToString();
        stars.fillAmount = PlayerPrefs.GetFloat("Progress", 0f);
        Level1_1 = GameObject.Find("Level1_1");
        Level1_2 = GameObject.Find("Level1_2");
        Debug.Log(PlayerPrefs.GetInt("Coins").ToString());

    }


    public void Level11()
    {
        cam.transform.position = new Vector3(Level1_1.transform.position.x, Level1_1.transform.position.y, -10);
        Level1_2.SetActive(false);      
    }

    public void Level12()
    {
        cam.transform.position = new Vector3(Level1_2.transform.position.x, Level1_2.transform.position.y, -10);
        Level1_1.SetActive(false);
    }

And here is the part of LevelControl script where i want to check what last button is clicked:

if (diff == 0)
     {
         Invoke("LvlFinish", 2f);                  
         //Check what button is clicked and 
         // turn it off so that level can't be played again
                    
     }               

Upvotes: 0

Views: 544

Answers (1)

Alexis C. Gridel
Alexis C. Gridel

Reputation: 71

Maybe you can try something like this ?

    using UnityEngine;
    using System.Collections;
    using UnityEngine.EventSystems;
     
    public class MyButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {
     
    public bool buttonPressed;
     
    public void OnPointerDown(PointerEventData eventData){
         buttonPressed = true;
    }
     
    public void OnPointerUp(PointerEventData eventData){
        buttonPressed = false;
    }
    }

Upvotes: 1

Related Questions