[英]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.