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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Triggering raw image popup.

Discussion in 'Scripting' started by cristo, May 24, 2015.

  1. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Hi,
    I've put a box collider onto a nonplayer character in the hope of creating a GUI popup. I attached this script to the box collider and
    I drag-and-dropped Canvas from the hierarchy to the public game object in the script in the inspector.

    When the game scene is opened, the canvas switches off in the script in the inspector, but it doesn't switch back on when the player collides with the box collider trigger.

    I'm not sure what's going wrong. :confused:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PopScript : MonoBehaviour {
    5.  
    6.     public GameObject Canvas;
    7.    
    8.     void Start ()
    9.     {
    10.         Canvas = GameObject.Find ( "Canvas" );
    11.     }
    12.     void Update () {
    13.  
    14.         Canvas.GetComponent<Canvas>().enabled = false;
    15.     }
    16.     public void OnTriggerEnter ( Collider other )
    17.     {
    18.                 if (other.tag == "Player") {
    19.                         Canvas.GetComponent<Canvas> ().enabled = true;
    20.                 }
    21.         //else {
    22.         //                Canvas = GameObject.Find ("Canvas");
    23.         //                Canvas.GetComponent<Canvas> ().enabled = false;
    24.        
    25.                 //}
    26.         }
    27.    
    28. }
    29.  
     
  2. imosek8

    imosek8

    Joined:
    Apr 24, 2015
    Posts:
    10
    I think, that is becouse you switch off this canvas on each frame in your Update procedure.

    Try to make this canvas disabled in Start procedure, then in On Trigger Enter make it enabled, and then in OnTriggerExit make this canvas disabled.
     
    cristo likes this.
  3. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Yes, you're right!
    Thanks. :)