2016-03-06 71 views
0

我忙我自己的Unity3d游戏的工作,这里是其中一个脚本我目前有:Unity3d C#分配错误

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 

此错误是缠着我了,因为我至今都与所有其他基本相同脚本没有问题,所以我不知道w ^我在这里做错了什么?我该如何解决这个错误?

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

+0

尝试!(this.behaviour = ...) –

+0

我试过这个,这没有改变任何东西 –

+1

你正在混合一个布尔测试的assignement。我想你想要的东西像_if((this.behaviour = find ....)== null)返回false; _ – Steve

回答

2

由于@Steve指出你混合分配与布尔测试呼叫。可能这就是你想要做的。

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

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