1
\$\begingroup\$

I have tried everything but it still doesn't work. I am not talking about implementing functions wich has stuff inside when you click the UI Button. I want literally replace this Input.GetKeyDown(KeyCode.E). That is inside an if statement and replace with the UI Button object. How can i do this? I have tried with GetButton, GetButtonDown, trying to give a name and with Input.touchCount>0

I am developing a game like Resident Evil old style and when you enter a triggerStay and push the button, a text is showing up.

The Script

using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine;

public class readingNote : MonoBehaviour {


    public AudioSource audio;
    public AudioClip collectSound;
    public  bool playerNextToKey = false;
    bool hasCollided = false;
    public GameObject pic;
    public GameObject text;
    public GameObject notePad;

    public Button yourButton;

    void Start()
    {
        pic.SetActive(false);
        text.SetActive (false);
        Button btn = yourButton.GetComponent<Button>();
        btn.onClick.AddListener(TaskOnClick);
    }

    void TaskOnClick()
    {
/*THIS IS SHOWING ALL TEXT IN THE SCENE AND NOT ONE BY ONE WHEN PRESSING*/
//      text.SetActive (true);
//      pic.SetActive(true);
    }

    void OnTriggerStay ( Collider other) {


        if(other.gameObject.tag == "Player") {
            if(Input.GetKeyDown(KeyCode.E)) {
            //if(Input.touchCount>0) {
                text.SetActive (true);
                pic.SetActive(true);

                AudioSource.PlayClipAtPoint(collectSound, transform.position);  

            }   

        }
    }


    void  OnTriggerExit ( Collider other  )
    {
        if (other.gameObject.tag == "Player")
        {
            //enter = false;
            //print("close");

            playerNextToKey = false;
            hasCollided = false;
            pic.SetActive(false);
            text.SetActive (false);

        }
    }

}
\$\endgroup\$
  • \$\begingroup\$ You can do that directly in Unity: Click on your button, go to the inspector and to the Button component. There you should see an empty box titled "onClick". You can add an Event here and adjust which script/GameObject and which of it's functions shall be called. \$\endgroup\$ – derHugo Oct 16 '17 at 5:25
2
\$\begingroup\$

Simply add a boolean to indicate whether your player is "allowed" to press the button in order to activate your note :

using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine;

public class readingNote : MonoBehaviour
{
    public AudioSource audio;
    public AudioClip collectSound;
    public  bool playerNextToKey = false;
    bool hasCollided = false;
    public GameObject pic;
    public GameObject text;
    public GameObject notePad;

    public Button yourButton;

    void Start()
    {
        pic.SetActive( false );
        text.SetActive( false );
        yourButton.onClick.AddListener( TaskOnClick );
    }

    void TaskOnClick()
    {
        if( !hasCollided )
            return ;

        text.SetActive( true );
        pic.SetActive( true );

        AudioSource.PlayClipAtPoint( collectSound, transform.position );
    }

    void OnTriggerStay( Collider other )
    {
        if ( other.CompareTag("Player") )
        {
            hasCollided = true ;
        }
    }


    void OnTriggerExit( Collider other )
    {
        if ( other.CompareTag("Player") )
        {
            playerNextToKey = false;
            hasCollided = false;
            pic.SetActive( false );
            text.SetActive( false );
        }
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.