Hello, i have this error in my script : NullReferenceException: Object reference not set to an instance of an object.
It doesn’t really bother me since I can put play and play normally. But I would like to know what error is causing this problem. Thanks again for your help!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class turbo : MonoBehaviour
{
public ParticleSystem SetParticles;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(1))
{
SetParticles.Play();
}
}
}
Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.
This is the kind of mindset and thinking process you need to bring to this problem:
I have run the script about 30 times removing and modifying a single parameter each time. I think the problem is with public ParticleSystem SetParticles; because it cannot find ParticleSystem in the script.
Is your particle system assigned to the field in the Inspector on your script?
A Null Reference Exception (NRE) is caused when you’re trying to access a method or property on a null object – in other words, trying to get something from nothing.
Pretend I give you a set of instructions:
I will hand you a package of Oreos.
Open the package
Take out a cookie.
Eat the cookie.
Close the package.
I hand you the package, you open it, and the container is empty. Your “code” would fail at line 4, because you’d be trying to eat a cookie that doesn’t exist. That’s an NRE.
@Kurt-Dekker gave you some great tips on how to find that problem. @MartinMa just straight up gave you the answer. What part are you having difficulty understanding?
You’re right! The first time I used this analogy, I was using a single object. The idea being that fetching that object didn’t throw an error, but returned null. I’ve updated the cookie example to reflect that thinking.