我需要帮助,我正在尝试将一个脚本中的浮点数添加到另一个脚本中,但是它不起作用。我是C#和编码的新手,如果有人代码修复了脚本并解释了这是怎么回事,我将非常感激。这是我得到的错误。 “ NullReferenceException:对象引用未设置为对象的实例Resources.Die()(在Assets / Resources.cs:42处)Resources.Update()(在Assets / Resources.cs:22处)”这是我的脚本:1st
using 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 () {
}
}
相关分类