简体   繁体   English

尝试从另一个脚本调用 function 会引发 NullReferenceException 错误

[英]Trying to call a function from another script throws the NullReferenceException error

I am calling a function from a different script script and I am getting this error "NullReferenceException: Object reference not set to an instance of an object PlayerCollision.OnTriggerEnter (UnityEngine.Collider collision)" What do I do? I am calling a function from a different script script and I am getting this error "NullReferenceException: Object reference not set to an instance of an object PlayerCollision.OnTriggerEnter (UnityEngine.Collider collision)" What do I do? This is my code.这是我的代码。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerCollision : MonoBehaviour
{

void OnTriggerEnter(Collider collision)
{ 
    
    if(collision.gameObject.CompareTag("BlackSnow"))
        {
            GameManager.Instance.blackSnowEvent(collision.gameObject);
        }
    }
}

this is the code in the script where the function is:这是脚本中的代码,其中 function 是:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour
{

public ParticleSystem blackSnowParticleSystem;
private bool snowActivated = false;
public int duration = 5;


#region Singleton

private static GameManager instance;

public static GameManager Instance
{
    get
    {
        return instance;    
    }
}


void Start()
{
}

 IEnumerator wait(int arg){
    yield return new WaitForSeconds(arg);
}

public void blackSnowEvent(GameObject snowTrigger){
    if(snowActivated == false){

        snowActivated = true;
        snowTrigger.SetActive(false);

        blackSnowParticleSystem.Play();
        Debug.Log(duration);
        StartCoroutine(wait(duration));

        blackSnowParticleSystem.Stop();
       
        snowActivated = false;
    }
} 
#endregion
}

The member GameManager.Instance is never assigned, so it is null at runtime.成员GameManager.Instance从未分配,因此在运行时为 null。 You are attempting to invoke the member blackSnowEvent on a null reference.您正尝试在 null 引用上调用成员blackSnowEvent To solve, GameManager.Instance must be assigned before you attempt to invoke its members要解决此问题,必须在尝试调用其成员之前分配GameManager.Instance

The error is not because you are calling a method from a different script, it is because you are trying to pass a null value as an argument to it.该错误不是因为您从不同的脚本调用方法,而是因为您试图将null值作为参数传递给它。

The error is because in your if statement in your void OnTriggerEnter(Collider collision) method, you are trying to check if the collided object has a tag "BlackSnow" or not.该错误是因为在您的void OnTriggerEnter(Collider collision)方法中的if语句中,您试图检查碰撞的 object 是否具有标签“BlackSnow” But that collider doesn't necessarily be something, it may also be null.但是那个对撞机不一定是什么东西,也可能是null。 Hence, Hence modify the if statement to this.因此,因此将if语句修改为此。

if(collision != null && collision.gameObject.CompareTag("BlackSnow")) {
    GameManager.Instance.blackSnowEvent(blackSnowParticleSystem, collision.gameObject);
}

In this scenario, if the compiler detects that the collision object is null, then it doesn't proceed further, therefore doesn't throw any error.在这种情况下,如果编译器检测到碰撞 object 是 null,则它不会继续进行,因此不会抛出任何错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从另一个脚本在 CustomEditor 脚本中调用 function - Call a function in a CustomEditor script from another script 从另一个脚本访问数组的元素时发生NullReferenceException - NullReferenceException when accessing to an element of array from another script 我如何从另一个脚本调用 object,尝试放入 SetActive function 并且它不喜欢“静态”,对于 Unity/C# 来说非常新 - How do I call an object from another script, trying to put in the SetActive function and it doesnt like "static", VERY NEW to Unity/C# 尝试从静态类获取数据时,Unity中出现NullReferenceException错误 - NullReferenceException error in Unity when trying to get data from a static class 从另一个脚本访问start()函数错误? - Accessing start () function from another script error ? C#:尝试从另一个窗体调用UserControl函数 - C# : Trying to call a UserControl function from another Form 尝试对!IsPostBack进行FindControl的NullReferenceException错误 - NullReferenceException error trying to FindControl on !IsPostBack 自定义聚合函数间歇性地引发NullReferenceException - Custom aggregate function intermittently throws NullReferenceException 从另一个对象访问Form组件将引发“未处理System.NullReferenceException” - accessing a Form component from another object throws “System.NullReferenceException was unhandled” 尝试运行单元测试时,HttpContextBaseExtensions.GetOwinContext引发NullReferenceException - HttpContextBaseExtensions.GetOwinContext throws NullReferenceException when trying to run Unit tests
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM