Clicking Cube And Canvas Appears and disappears
Hi! I have a question and I'm really having difficulty with this. I want to click a cube(GameObject) then the canvas pops up and then when I click on any screen, the canvas disappears. If Canvas's name = keypadCanvas Cube's name = keypad What should I do?
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MenuAppearScript : MonoBehaviour {
       
     public Canvas keypadCanvas;
     private bool popup;
 
     void Start()
     {
         popup = false;
     }
 
     void Update()
     {
         if (Input.GetMouseButtonDown (0)) {
             popup = !popup;
             Canvas.enabled
         }
     }
 }
 
               When I use keypadCanvas.SetActive, it doesn't work... Maybe Canvas is not Gameobject... I'm really confused please help me!!
Answer by Hellium · Nov 07, 2017 at 04:59 PM
The following script should work :
  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class MenuAppearScript : MonoBehaviour {
        
      public Collider cubeCollider; // Drag & Drop the cube where a collider is attached
      public Canvas keypadCanvas; // Drag & drop the canvas to enable / disable
  
      void Update()
      {
          if (Input.GetMouseButtonDown (0))
          {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             keypadCanvas.SetActive( cubeCollider.Raycast(ray, out hit, 100.0F) ) ;
          }
      }
  }
 
              This makes an error. UnityEngine.Canvas does not contain a definition for 'SetActive'. SetActive is always problem..
I'm pretty sure that Canvas is not a gameobject. $$anonymous$$aybe that's why it doesn't work with SetActive. Any other way?
Yes, my bad. You have to call keypadCanvas.gameObject.SetActive( cubeCollider.Raycast(ray, out hit, 100.0F) ) ; 
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class $$anonymous$$enuAppearScript : $$anonymous$$onoBehaviour {
 public Collider cubeCollider; // Drag & Drop the cube where a collider is attached
 public Canvas keypadCanvas; // Drag & drop the canvas to enable / disable
 void Start()
 {
     keypadCanvas.gameObject.SetActive (false);
 }
 void Update()
 {
     if (Input.Get$$anonymous$$ouseButtonDown (0)) {
         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         RaycastHit hit;
         keypadCanvas.gameObject.SetActive( cubeCollider.Raycast(ray, out hit, 100.0F) ) ; 
     }
 }
 
                    }
I added a little and it works perfect. Thx!!
Your answer