c# - 使用附加到触发器的此脚本时出现 NullReferenceException
问题描述
我正在使用附加到设置为触发器的 GameObject 的此脚本,但我无法解决此 NullReferenceException 错误:
对象引用未设置为对象的实例。IntExt_Trigger.awake() (在 Assets/scripts/IntExt_Trigger.cs)
我不知道是什么原因造成的……因为据我所知,唤醒功能正在正确应用,有什么我完全遗漏的吗?
这是脚本。
using System.Collections.Generic;
using UnityEngine;
public class IntExt_Trigger : MonoBehaviour
{
private AudioSource ACinside, Blizzard_from_INT, Blizzard_from_INT_2, rain;
private AudioSource AirlockSealOpen_HV, AirlockSealClose_HV;
private AudioReverbZone Int_Reverb_Zone, Ext_Reverb_Zone;
private AudioSource ext_wind;
private void Awake()
{
ACinside = GameObject.Find("ACinside").GetComponent<AudioSource>();
Blizzard_from_INT = GameObject.Find("Blizzard_from_INT").GetComponent<AudioSource>();
Blizzard_from_INT_2 = GameObject.Find("Blizzard_from_INT_2").GetComponent<AudioSource>();
AirlockSealOpen_HV = GameObject.Find("AirlockSealOpen_HV").GetComponent<AudioSource>();
AirlockSealClose_HV = GameObject.Find("AirlockSealClose_HV").GetComponent<AudioSource>();
Int_Reverb_Zone = GameObject.Find("Int_Reverb_Zone").GetComponent<AudioReverbZone>();
Ext_Reverb_Zone = GameObject.Find("Ext_reverb_Zone").GetComponent<AudioReverbZone>();
ext_wind = GameObject.Find("ext_wind").GetComponent<AudioSource>();
}
//this section above defines the variables and functions//
private void OnTriggerEnter(Collider other)
{
// for OnTriggerEnter to run the trigger and the player model objects must have colliders//
// the folowing actions are executed when an entrance or collision to the trigger is detected//
Int_Reverb_Zone.enabled = false;
if (ext_wind.isPlaying == true)
{
ACinside.Stop();
Blizzard_from_INT.Stop();
rain.Stop();
Int_Reverb_Zone.enabled = false;
Ext_Reverb_Zone.enabled = true;
ext_wind.Play();
}
else
{
ACinside.Play();
Blizzard_from_INT.Play();
Blizzard_from_INT_2.Play();
rain.Play();
Int_Reverb_Zone.enabled = true;
Ext_Reverb_Zone.enabled = false;
ext_wind.Stop();
}
}
}
解决方案
你应该验证你得到的所有组件,像这样:
GameObject?.Find("ACinside")?.GetComponent<AudioSource>();
要么GameObject
或ACinside
对象为空。
推荐阅读
- html - 为什么从选取的文件中返回值是 null ?扑
- vba - 从 Outlook 中的网站超链接下载文件
- html - 滚动后背景不扩展
- python - http请求json的这种速度差异正常吗?
- kibana - Kibana加载131个js文件正常吗?
- python - 在 Jupyter Notebook 中以国家地图为基础绘制数据
- javascript - 如何从方法返回异步值?
- reactjs - 输入'{孩子:从不[]; }' 与类型 'IntrinsicAttributes' 没有共同的属性。尝试渲染材质 UI 时的 Typescript + React 错误
- graphviz - graphviz 折叠平行路径
- c# - 使用 Bootstrap 3 alpha,Accordion 不仅仅在 Edge 上打开。在 Firefox 和 Chrome 上运行良好