我需要帮助,我想尝试将浮点数从脚本添加到另一个脚本但是它不起作用。我是c#和编码的新手,如果有人代码修复脚本并解释了什么错误,我会非常感激。这是我得到的错误。 “NullReferenceException:对象引用未设置为对象的实例 Resources.Die()(在Assets / Resources.cs:42) Resources.Update()(在Assets / Resources.cs:22)“ 这是我的脚本: 第一
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 () {
}
}
答案 0 :(得分:2)
通过查看您的代码,我认为您从未为“资源”脚本定义库存实例。如果您使用的是getcomponent,则资源和库存脚本必须位于同一个游戏对象上才能找到。 如果您想要的是两个脚本都在其他游戏对象上,则需要在资源脚本中引用库存。 您可以通过多种方式执行此操作(例如,创建静态引用并引用它或将库存定义为公共变量,然后在统一编辑器中添加库存引用)
这是第一种情况的样子:
第一个脚本 -
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Inventory : MonoBehaviour
{
public static Inventory instance;
private void Awake()
{
instance = this;
}
public float ResourceStone;
// Use this for initialization
void Start()
{
ResourceStone = 0;
}
}
第二个脚本 -
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.instance.ResourceStone++;
}
}
}
如果你做了第二个代码,这就是代码的样子:
第一个脚本:
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()
{
}
}
第二个脚本 -
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Resources : MonoBehaviour
{
public float maxHealth = 5;
public float currentHealth;
public float ResourceCounter;
public Inventory inventory;
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.ResourceStone++;
}
}
}
只考虑两件事来决定你是使用第一个还是第二个。第一个是静态引用,这意味着您无法创建更多的库存。在第二个中,您需要在编辑器中手动拖动引用。如果您不知道,在整数后写++与整数=整数+ 1;
相同 编辑:那里,看起来更好看:D