【问题标题】:Unity Public GameObject CS0117Unity 公共游戏对象 CS0117
【发布时间】:2016-04-21 08:41:45
【问题描述】:

这是我的脚本

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;

}
} 

当我改变时。公共画布到公共游戏对象

public GameObject myObject;

    // Use this for initialization
    void Start () {

        myObject.enabled = false;
    }

在 myObject.enabled 中是红色文本 并说“错误 CS0131:赋值的左侧必须是变量、属性或索引器”

为什么?

更新问题-------

最重要的问题是如何改变

public Canvas myCanvas;

public GameObject myCanvas;

myCanvas.enabled = false;

确定错误。因为游戏对象不需要启用

但这是我的真实脚本

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!");
        }
    }

它在什么时候起作用

public Canvas Model_1;

启用。

但是当我想将 Canvas 更改为 GameObject 时如何?

我必须在这里改变什么

public GameObject Model_1;

model_1.enabled=false;

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

因为我的模型不只是 1 所以我可以像 LOGIC 一样改变我的对象 如果(模型_1 假) model_1 开启 再次单击 BtnLeft (如果 model_1 开启) 模型_1 假 model_2 开启 像下一个对象

【问题讨论】:

    标签: c# unity3d augmented-reality vuforia


    【解决方案1】:

    这不是一个难题。因为游戏对象没有 enabled 属性。

    您需要做的是将代码更改为:

    myCanvas.SetActive(false);

    我对学习 unity3d 的建议是阅读更多文档并观看更多教程。 Evem 是非常基本的。

    附言

    Google is a better teacher than SO.
    

    如果你想让开关起作用,看来你的逻辑是对的。您只需将代码添加到Update

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

    【讨论】:

    • if (myCanvas.enabled) myCanvas.enabled = false;否则 myCanvas.enabled = true; ||当我想打开/关闭时,我必须在这里改变什么
    • 您的OnMouseDown 是私有方法,它不会运行。逻辑是对的。也许您可以添加新代码以回答Update
    • 看我先改变我的问题,但仍然有联系
    • 对不起,我很久没有在 vuforia 上工作了。我想你可以尝试去 vuforia 论坛找到关于虚拟按钮的解决方案。
    • 另一个建议是重新问一个关于 vuforia 虚拟按钮的新问题
    【解决方案2】:

    我知道这有点晚了,但我想如果您仍有问题,您可能仍需要帮助。首先,您不能使用.enabled 禁用游戏对象,因为.enabled 仅适用于组件,而不适用于游戏对象。要禁用游戏对象,您需要使用SetActive。所以你会这样做:

    myObject.SetActive(false);
    

    现在在 tim 的帖子上回答您的 cmets。你目前有这个;

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

    您必须使用activeSelf。如果 GameObject 处于活动状态,这将返回 true。所以改成这样:

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

    【讨论】:

    • 我喜欢人们分享知识。有效的先生!我的问题不仅仅是 vuforia。它只是如何在公共游戏对象中使用 if,因为启用是红色文本(错误)。谢谢你非常匹配解决了问题
    猜你喜欢
    • 1970-01-01
    • 2019-04-20
    • 1970-01-01
    • 2017-12-13
    • 2016-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多