0

So I'm getting an error which says

Object reference not set to an instance of an object Explode.LateUpdate () (at Assets/Scripts/Explode.cs:40)

I'm not sure why I get this error as everything works fine and I have the exact same script in a different scene without the error message. I'd love any help please.

I'm exploding my player with different coloured boxes.

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();
        }
    }
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • 1
    Your error is on line 40 as the message say, make sure everything that is called is instantiated in this line. – S.Fragkos Apr 20 '18 at 06:37

2 Answers2

2

If this is line 40 (I had to count):

ParticleSystem.Particle[] Particles = new ParticleSystem.Particle[explosionSystem.main.maxParticles];

Then explosionSystem or main or maxParticles is null. I'm going to close this as a duplicate, this error is so common and the way to resolve it is easy.

But a break point on the line of code, when the code control halts, check which object is null and make sure its set.

EDIT:

Use the GetComponent to initialize the explosionSystem variable in the Start function.First, find the GameObject the ParticleSystem is attached to the use GetComponent to get the ParticleSystem.

private ParticleSystem explosionSystem;
void Start()
{
    GameObject obj = GameObject.Find("NameOfObjectParticleSystemIsAttachedTo");
    explosionSystem = obj.GetComponent<ParticleSystem>();
}

Also, inside the for loop, you tried to use Particles's startColor property without initializing each one. You can do that with Particles[i] = new ParticleSystem.Particle();.

Change:

for (int i = 0; i < NumParticlesAlive; i++)
{
    Particles[i].startColor 
 = (ColoursList[PlayerPrefs.GetInt("SelectedChar")].Colours[Random.Range(0, ColoursList[PlayerPrefs.GetInt("SelectedChar")].Colours.Length)]);
}

to

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)]);
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • Thanks for your reply and yes I know this is a common error but the script works fine in one scene and I get an error in another. Everything that I can see is set so I can't see why it won't work. – Toby Langdon Apr 20 '18 at 07:34
  • 1
    Please, please, please do yourself a huge favour and learn how to debug code. Your `explosionSystem` is null. You need to work out why it wasn't instantiated. We can't do this for you without all the source code. Watch some debugging videos. This is a good/easy problem to learn debugging to solve: put the code in a try catch, put a breakpoint on the Catch, when the exception occurs, hover your mouse over the variables making sure everything is set. You've missed something. Ps If I reopened this you'll likely get downvotes and then someone else will close it as a duplicate anyway. – Jeremy Thompson Apr 20 '18 at 08:29
  • 1
    I @Jeremy and Toby, I have modified to add here OP has messed up and the fix. Hopefully, your problem is solved. – Programmer Apr 20 '18 at 13:20
0

To avoid the null reference error, make sure all variables (which are using) has values other wise at least assign default values in constructor level