我得到這個錯誤在Unity:的NullReferenceException不能找出什麼是空
的NullReferenceException:未設置爲一個對象 TowerSlot.OnGUI(實例對象引用)(在資產/ TowerSlot.cs:26)
我是相對較新的統一,無法弄清楚這個錯誤是從哪裏來的(我假設26),我不知道什麼是空的。如果有人可以請幫助向我解釋如何理解錯誤指向什麼以及我需要做什麼,我將非常感謝。
TowerSlot.cs:
using UnityEngine;
using System.Collections;
public class TowerSlot : MonoBehaviour {
public GUISkin skin = null;
bool gui = false;
// Tower prefab
public Tower towerPrefab = null;
void OnGUI() {
if (gui) {
GUI.skin = skin;
// get 3d position on screen
Vector3 v = Camera.main.WorldToScreenPoint(transform.position);
// convert to gui coordinates
v = new Vector2(v.x, Screen.height - v.y);
// creation menu for tower
int width = 200;
int height = 40;
Rect r = new Rect(v.x - width/2, v.y - height/2, width, height);
GUI.contentColor = (Player.gold >= towerPrefab.buildPrice ? Color.green : Color.red);
GUI.Box(r, "Build " + towerPrefab.name + "(" + towerPrefab.buildPrice + " gold)");
// mouse not down anymore and mouse over the box? then build the tower
if (Event.current.type == EventType.MouseUp &&
r.Contains(Event.current.mousePosition) &&
Player.gold >= towerPrefab.buildPrice) {
// decrease gold
Player.gold -= towerPrefab.buildPrice;
// instantiate
Instantiate(towerPrefab, transform.position, Quaternion.identity);
// disable gameobject
gameObject.SetActive(false);
}
}
}
public void OnMouseDown() {
gui = true;
}
public void OnMouseUp() {
gui = false;
}
}
此外,我想在這裏學習本教程http://makeagame.info/unity-tower-defense-game-step-4-scripting
謝謝!
真棒,它修復它感謝。 – Ivatrix