我需要幫助,我正在嘗試將一個腳本中的浮點數添加到另一個腳本中,但是它不起作用。我是C#和編碼的新手,如果有人代碼修復了腳本并解釋了這是怎么回事,我將非常感激。這是我得到的錯誤。 “ NullReferenceException:對象引用未設置為對象的實例Resources.Die()(在Assets / Resources.cs:42處)Resources.Update()(在Assets / Resources.cs:22處)”這是我的腳本:1stusing System.Collections;using System.Collections.Generic;using UnityEngine;public class Resources : MonoBehaviour { public float maxHealth = 5; public float currentHealth; public float ResourceCounter; public Texture stoneIcon; public Texture woodIcon; void Start () { currentHealth = maxHealth; ResourceCounter = 0; } void Update () { Die(); } public void OnMouseOver() { if(Input.GetButtonDown("Fire1")) { Debug.Log("Loosing one health"); currentHealth = currentHealth - 1f; } } public void Die() { if(currentHealth <= 0) { Destroy(gameObject); Inventory inventory = GetComponent<Inventory>(); inventory.ResourceStone = inventory.ResourceStone + 1; } }}第二名using System.Collections;using System.Collections.Generic;using UnityEngine;public class Inventory : MonoBehaviour { public float ResourceStone; // Use this for initialization void Start () { ResourceStone = 0;} // Update is called once per frame void Update () { }}
- 1 回答
- 0 關注
- 153 瀏覽
添加回答
舉報
0/150
提交
取消