2016-03-06 32 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編輯器將引用分配給其他組件,而不是搜索它們。