繁体   English   中英

Unity3d C#分配错误

[英]Unity3d C# assignment error

我正在忙于自己的Unity3d游戏,这是我目前拥有的脚本之一:

HungerHandler.cs

using UnityEngine;
using System.Collections;
using ProgressBar;

public class HungerHandler : MonoBehaviour {

    private ProgressBarBehaviour behaviour;
    public bool Start () {
        if (!this.behaviour = GameObject.Find("hungerBar").GetComponent<ProgressBarBehaviour>()) {
            return false;
        }
        this.behaviour.Value = 0f;
        return true;
    }

    public bool Add(int percentage){
        if (this.behaviour.Value < 100) {
            this.behaviour.Value = this.behaviour.Value + percentage;
            return true;
        }
        return false;
    }

    public bool Damage(int percentage){
        if ((this.behaviour.Value - percentage) > 0) {
            this.behaviour.Value = this.behaviour.Value - percentage;
            return true;
        }
        return false;
    }
}

当我尝试运行游戏时,出现错误消息:

Assets/custom/handlers/HungerHandler.cs(9,27): error CS0131: The left-hand side of an assignment must be a variable, a property or an indexer

因为到目前为止我对所有其他基本相同的脚本都没有问题,所以这个错误使我烦恼,所以我想知道我在这里做错了什么吗? 我该如何解决此错误?

亲切的问候,乔凡尼·勒格兰德

正如@Steve指出的那样,您正在将赋值调用与布尔测试混在一起。 可能这就是您想要做的。

this.behaviour = GameObject.Find("hungerBar").GetComponent<ProgressBarBehaviour>();
if (this.behaviour == null) {
    return false;
}

但是这是另一回事。 由于您正在使用MonoBehaviour s-最好通过Unity3d Editor引用将其分配给其他组件,而不是搜索它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM