1
\$\begingroup\$

I am trying to show a text when i click on a ui button but i don't know how to implement this. On PC platform I have a collider per object and when the player stay and click the E button, a canvas text appear on screen, but i don't know how to do this on Android. I want something like Resident Evil old style action button, opening doors and getting object description. How could i do this?

The Script

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;
    private Collider other;


//  void Start () {
//      pic.SetActive(false);
//      text.SetActive (false);
//
//  }


    public Button yourButton;

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

    void TaskOnClick()
    {
      //can't text.SetActive(true); because when i get out the scene, text is showing up (I use the same button to open doors and get text description).
    }

    void OnTriggerStay ( Collider other) {


        if(other.gameObject.tag == "Player") {
            //if(Input.GetKeyDown(KeyCode.E)) {
                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\$
1
\$\begingroup\$

as you said in your comments you need a system that shows specific info in specific trigger. you can do it in many different ways. right now using something like a dictionary is in my mind. first of define triggers and a variable in class of your character. I write it as psudo code so it may need some changes.

public GameObject   canvasText; //assign ui text game object in editor

string mystring;

    void start()
{
Dictionary<string, string> mydic =  new Dictionary<string, string>();
mydic.add("x1","open the door");
mydic.add("x2","pic up the pistol")
}

void onTriggerEnter(collider other)
{
mystring=mydic[other.name];
}

void onTriggerExit(collider other)
{
mystring="";
canvasText.getcomponent<Text>().text="";
}

    public void UIbutton()// assign this function to function of your button in editor
    {
canvasText.getcomponent<Text>().text=mystring;
    vanvasText.setActive(true);
    }
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I have tried that and it works, the thing is, the UI button is an action button that open doors and read texts, so when i go out the scene, the text is showing up too. \$\endgroup\$ – Jose Oct 15 '17 at 19:58
  • \$\begingroup\$ @can you explain more? what is you problem exactly? \$\endgroup\$ – virtouso Oct 15 '17 at 20:02
  • \$\begingroup\$ My game is like Resident Evil old style and when i'm close to an object and press UI button, a description of that object appear on screen. Everything is fine at this point, but if i use your solution, the UI description of all obejcts of scene are showing on screen when i change scene because i am using the same button for all (change scene and get object descriptions). \$\endgroup\$ – Jose Oct 15 '17 at 20:10
  • \$\begingroup\$ @Jose now you question is clear. there are lot of approaches that you can handle your problem. ill try editing my answer \$\endgroup\$ – virtouso Oct 15 '17 at 20:16

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.