Problem with gameobject (MissingComponentExeption).. (Problem Solved)

Click cube > show/hide canvas include text.
I’ve put this script into the cube, and when I click the cube it will show the Canvas. I haven’t been clicking on the cube but, the canvas has already come out. and now have problem

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class MouseDownText : MonoBehaviour {
  
    public Canvas myCanvas;
  
    // Use this for initialization
    void Start () {
      
        myCanvas = GetComponent<Canvas> ();
        myCanvas.enabled = false;
    }
  
    // Update is called once per frame
    void Update () {
      
    }
  
    void OnMouseDown()
    {
        // for switch on/off
      
        if (myCanvas.enabled)
            myCanvas.enabled = false;
        else
            myCanvas.enabled = true;
    }
}

in cube. canvas already in cube.

You are trying to GetComponent() into myCanvas variable. And it fails. While not using value you set myCanvas in inspector.

You don’t need GetComponent line at all.

work !! i delete get.component it work
really genius answer, thank you :slight_smile: