C# 脚本在一个场景中工作,但在另一个场景中出错
所以我得到一个错误,它说 对象引用未设置为对象Explode.LateUpdate()的实例(位于Assets/Scripts/Explode.cs:40) 我不知道为什么会出现这个错误,因为一切正常,我在不同的场景中有完全相同的脚本,没有错误消息。我想要任何帮助 我正在用不同颜色的盒子引爆我的播放器C# 脚本在一个场景中工作,但在另一个场景中出错,c#,unity3d,C#,Unity3d,所以我得到一个错误,它说 对象引用未设置为对象Explode.LateUpdate()的实例(位于Assets/Scripts/Explode.cs:40) 我不知道为什么会出现这个错误,因为一切正常,我在不同的场景中有完全相同的脚本,没有错误消息。我想要任何帮助 我正在用不同颜色的盒子引爆我的播放器 using System.Collections; using System.Collections.Generic; using UnityEngine; public class E
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Explode : MonoBehaviour {
[System.Serializable]
public class ExplodeColours
{
public Color32[] Colours;
}
public static bool explode, explodeOnce;
public ParticleSystem Explodes;
private ParticleSystem explosionSystem;
public Transform player;
public List<ExplodeColours> ColoursList;
// Use this for initialization
void Start () {
PlayerPrefs.SetInt("SelectedChar", 0);
PlayerPrefs.Save();
explodeOnce = false;
explode = false;
}
// Update is called once per frame
void Update () {
if (explode == true)
{
explodeOnce = true;
PlayerExplode();
}
}
void LateUpdate()
{
if (explode)
{
ParticleSystem.Particle[] Particles = new ParticleSystem.Particle[explosionSystem.main.maxParticles];
//ParticleSystem.Particle[] Particles = new ParticleSystem.Particle[Explodes.main.maxParticles];
int NumParticlesAlive = explosionSystem.GetParticles(Particles);
for (int i = 0; i < NumParticlesAlive; i++)
{
Particles[i].startColor = (ColoursList[PlayerPrefs.GetInt("SelectedChar")].Colours[Random.Range(0, ColoursList[PlayerPrefs.GetInt("SelectedChar")].Colours.Length)]);
}
explosionSystem.SetParticles(Particles, NumParticlesAlive);
explode = false;
}
}
void PlayerExplode()
{
explosionSystem = Instantiate(Explodes, player.position, player.rotation);
explosionSystem.Play();
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共阶层爆炸:单一行为{
[系统可序列化]
公共类颜色
{
公共颜色32[]种颜色;
}
公共静态布尔爆炸,爆炸一次;
公共参与制度爆炸;
私有粒子系统爆炸系统;
公共转换播放器;
公共列表颜色列表;
//用于初始化
无效开始(){
PlayerPrefs.SetInt(“SelectedChar”,0);
PlayerPrefs.Save();
一次=假;
爆炸=假;
}
//每帧调用一次更新
无效更新(){
如果(分解==真)
{
一次=真;
PlayerExplode();
}
}
void LateUpdate()
{
如果(爆炸)
{
ParticleSystem.Particle[]Particles=新的ParticleSystem.Particle[explosionSystem.main.maxParticles];
//ParticleSystem.Particle[]Particles=新的ParticleSystem.Particle[Explodes.main.maxParticles];
int NumParticlesAlive=explosionSystem.GetParticles(Particles);
for(int i=0;i
要避免空引用错误,请确保所有变量(正在使用的)具有其他方式的值,至少在构造函数级别分配默认值要避免空引用错误,请确保所有变量(正在使用的)具有其他方式的值,至少在构造函数级别分配默认值(如果这是第40行)(我不得不数):
然后explosionSystem
或main
或maxParticles
为空。我将以重复的方式关闭此项,此错误非常常见,解决方法也很简单
但是代码行上的断点,当代码控件停止时,检查哪个对象为null并确保其已设置
编辑:
使用GetComponent
在Start
函数中初始化explosionSystem
变量。首先,找到ParticleSystem
附加到UseGetComponent
的游戏对象,以获取ParticleSystem
private ParticleSystem explosionSystem;
void Start()
{
GameObject obj = GameObject.Find("NameOfObjectParticleSystemIsAttachedTo");
explosionSystem = obj.GetComponent<ParticleSystem>();
}
到
for(int i=0;i
如果这是第40行(我必须计数):
然后explosionSystem
或main
或maxParticles
为空。我将以重复的方式关闭此项,此错误非常常见,解决方法也很简单
但是代码行上的断点,当代码控件停止时,检查哪个对象为null并确保其已设置
编辑:
使用GetComponent
在Start
函数中初始化explosionSystem
变量。首先,找到ParticleSystem
附加到UseGetComponent
的游戏对象,以获取ParticleSystem
private ParticleSystem explosionSystem;
void Start()
{
GameObject obj = GameObject.Find("NameOfObjectParticleSystemIsAttachedTo");
explosionSystem = obj.GetComponent<ParticleSystem>();
}
到
for(int i=0;i
您的错误在第40行,如消息所示,请确保调用的所有内容都在这一行中实例化。您的错误在第40行,如消息所示,请确保调用的所有内容都在这一行中实例化。感谢您的回复,是的,我知道这是一个常见错误,但脚本在一个场景中运行良好,我在一个场景中遇到错误另外。我能看到的一切都设置好了,所以我看不出它为什么不起作用。请,请,请帮自己一个大忙,学习如何调试代码。你的explosionSystem
为空。你需要找出它为什么没有实例化。没有所有的源代码,我们无法为你做到这一点。观看一些调试视频。这是一个好的/简单的pr学习调试要解决的问题:将代码放入一个try catch,在catch上设置一个断点,当异常发生时,将鼠标悬停在变量上,确保所有内容都已设置好。您错过了一些内容。Ps如果我重新打开此文件,您可能会获得否决票,然后其他人会将其作为副本关闭。I@Jeremy和Toby,我有修改为添加到这里OP已经搞砸了,并且修复了。希望你的问题已经解决。谢谢你的回复,是的,我知道这是一个常见的错误,但是脚本在一个场景中运行良好,而我在另一个场景中出现错误。我能看到的一切都设置好了,所以我看不出它为什么不起作用。请,请,请帮自己一个大忙,并学习如何删除错误代码。你的explosionSystemfor (int i = 0; i < NumParticlesAlive; i++)
{
Particles[i].startColor
= (ColoursList[PlayerPrefs.GetInt("SelectedChar")].Colours[Random.Range(0, ColoursList[PlayerPrefs.GetInt("SelectedChar")].Colours.Length)]);
}
for (int i = 0; i < NumParticlesAlive; i++)
{
Particles[i] = new ParticleSystem.Particle();
Particles[i].startColor
= (ColoursList[PlayerPrefs.GetInt("SelectedChar")].Colours[Random.Range(0, ColoursList[PlayerPrefs.GetInt("SelectedChar")].Colours.Length)]);
}