当前位置:网站首页>Unity FAQ (I) lack of references

Unity FAQ (I) lack of references

2022-07-07 22:31:00 Dimensional survivor

Problem description

When an object is called Unity An error message appears indicating that the reference is empty .

This is Unity The most common problem in English , Usually, the error message is

NullReferenceException: Object reference not set to an instance of an object

perhaps

UnassignedReferenceException

Question why

Usually, it is because the script defines some requirements in unity Manually drag and drop the mounted object in the , But it is not mounted . Or you want to get a component of an object, but the object does not carry the component . It is also possible that an object is destroyed during the run , But the object is still being called .

  resolvent

Find the missing reference object , Mount correctly .

Sample questions

For example, here we want the character controller (PlayerController) Generate a bullet (Bullet), But forget to hang the bullet preform to the script we need .

public class PlayerController : MonoBehaviour
{
    public GameObject bullet;

    void Start()
    {
        Instantiate(bullet);
    }
}

 

The content of the error report is as follows

Problem example 2

The correct mount has been performed here , We want to call the... On the bullet object Bullet Within the script component Fly function .

public class PlayerController : MonoBehaviour
{
    public GameObject bullet;

    void Start()
    {
        bullet.GetComponent<Bullet>().Fly();
    }
}

However, the bullet object does not have the corresponding script component attached , This causes the acquisition to fail .

 

 

 

原网站

版权声明
本文为[Dimensional survivor]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130604579844.html