0
votes

this is my script

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

    public class MouseDownText : MonoBehaviour {

        public Canvas myCanvas;

        // Use this for initialization
        void Start () {

            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;

}
} 

when i change. public Canvas to public GameObject

public GameObject myObject;

    // Use this for initialization
    void Start () {

        myObject.enabled = false;
    }

in myObject.enabled is red text and say "error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer"

why ?

UPDATE QUESTION -------

top question just how to change

public Canvas myCanvas;

to

public GameObject myCanvas;

with

myCanvas.enabled = false;

sure error. because gameobject no need Enabled

but here is my real script

using UnityEngine;
using System.Collections.Generic;
using Vuforia;

public class VirtualButtonEventHandler : MonoBehaviour, IVirtualButtonEventHandler {

    // Private fields to store the models
    public Canvas model_1;

    void Start() {
        // Search for all Children from this ImageTarget with type VirtualButtonBehaviour
        VirtualButtonBehaviour[] vbs = GetComponentsInChildren<VirtualButtonBehaviour> ();
        for (int i = 0; i < vbs.Length; ++i) {
            // Register with the virtual buttons TrackableBehaviour
            vbs [i].RegisterEventHandler (this);
        }



        model_1.enabled=false;
    }
        public void OnButtonPressed(VirtualButtonAbstractBehaviour vb) {
            //Debug.Log(vb.VirtualButtonName);
            Debug.Log("Button pressed!");

            switch(vb.VirtualButtonName) {
            case "btnLeft":
            if (model_1.enabled)
                model_1.enabled = false;
            else
                model_1.enabled = true;

                break;
                //  default:
                //  throw new UnityException("Button not supported: " + vb.VirtualButtonName);
                //  break;
            }

        }

        /// Called when the virtual button has just been released:
        public void OnButtonReleased(VirtualButtonAbstractBehaviour vb) {
            Debug.Log("Button released!");
        }
    }

it work when

public Canvas Model_1;

with Enabled.

but how when i want change Canvas to GameObject ?

what must i change in here

public GameObject Model_1;

and

model_1.enabled=false;

and

switch(vb.VirtualButtonName) {
                case "btnLeft":
                if (model_1.enabled)
                    model_1.enabled = false;
                else
                    model_1.enabled = true;

because my model it not just 1 so i can change my object like LOGIC if if(model_1 false) model_1 on click BtnLeft again (if model_1 on) model_1 false model_2 on like a next object

2

2 Answers

1
votes

It is not a difficult problem. Because gameobject does not has a enabled property.

What you need to do is to change code to:

myCanvas.SetActive(false);

My advice to learn unity3d is to read more doc and watch more tutorials. Evem the very basic ones.

P.S.

Google is a better teacher than SO.

If you want to make the switch work, It seemed that your logic is right. You just need to add the codes to Update.

void Update(){
    if(Input.GetMouseDown(0)){
        OnMouseDown();
    }
}
0
votes

I know this is a little late but I figured if you were still having problems you might still want help. First, you cannot disable GameObjects with .enabled, because .enabled is only for Components, not GameObjects. To disable a GameObject you need to use SetActive. So you would do:

myObject.SetActive(false);

Now to answer your comments on tim's post. You currently have this;

if (myCanvase.enabled)
{
    myCanvas.enabled = false;
}
else
{
    myCanvas.enabled = true;
}

You must use activeSelf. This returns true if the GameObject is active. So change it to this:

if (myCanvas.activeSelf)
{
    myCanvas.SetActive(false);
}
else
{
    myCanvas.SetAcive(true);
}