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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Help with trigger and canvas

Discussion in 'Scripting' started by Azinrr, Feb 14, 2023.

  1. Azinrr

    Azinrr

    Joined:
    Oct 6, 2022
    Posts:
    2
    Hey guys, I'm trying to do a code that trigger an object and calls a specific Canvas, like an interaction pressing space and showing a Canvas with a "Do you want to sleep (when close to a bed)? Yes or No (can be a button)".

    I did something similar just being on the trigger and it automatecally opens the canvas, but it's not the best solution, an interaction would be so much better for my project.

    I lost all my night on it, and still no results... since I'm a designer and not a programmer, it's so hard for me to understand it. Could someone help me? I tried a lot of codes and the last one is that one:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class TriggerDialogue : MonoBehaviour
    7. {
    8.     public GameObject scene;
    9.  
    10.     void OnTriggerEnter2D(Collider2D other)
    11.     {
    12.         if (Input.GetKeyDown(KeyCode.Z) && other.CompareTag("Player"))
    13.         {
    14.             scene.SetActive(true);
    15.         }
    16.     }
    17.  
    18.     void OnTriggerExit2D(Collider2D other)
    19.     {
    20.         if (other.CompareTag("Player"))
    21.         {
    22.             scene.SetActive(false);
    23.         }
    24.     }
    25. }
    Thank you in advance.
     
  2. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    490
    You need to listen for the key press in update, so use your trigger to set a bool to let update know it can listen for the key press.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class TriggerDialogue : MonoBehaviour {
    7.  
    8.     public GameObject scene;
    9.     bool playerIsInTriggerArea = false;
    10.     bool uiActive = false;
    11.  
    12.     void Update () {
    13.  
    14.         if (playerIsInTriggerArea && !uiActive) {
    15.             if (Input.GetKeyDown ("space")) {
    16.                 uiActive = true;
    17.                 scene.SetActive (true);
    18.             }
    19.         }
    20.  
    21.     }
    22.  
    23.     void OnTriggerEnter2D (Collider2D other) {
    24.  
    25.         if (other.CompareTag ("Player")) {
    26.             playerIsInTriggerArea = true;
    27.         }
    28.  
    29.     }
    30.  
    31.     void OnTriggerExit2D (Collider2D other) {
    32.  
    33.         if (other.CompareTag ("Player")) {
    34.             playerIsInTriggerArea = false;
    35.         }
    36.  
    37.     }
    38.  
    39. }
     
    Azinrr likes this.
  3. Azinrr

    Azinrr

    Joined:
    Oct 6, 2022
    Posts:
    2
    Thank you, worked perfecly fine! And it's good to learn it. Have a great day.
     
    QuinnWinters likes this.