- Home /
Question by
jason706 · Aug 22, 2015 at 10:47 PM ·
error message
Object reference error question
I'm still new to C, and am probably missing something basic. I have this script that keeps giving me this error: NullReferenceException: Object reference not set to an instance of an object ParticleCollide.OnParticleCollision (UnityEngine.GameObject other) (at Assets/Scripts/ParticleCollide.cs:35)
It compiles, and lets me run it, but everytime a collision occurs, I get the error above. I Bolded line 35 that's throwing the error.
using UnityEngine; using System.Collections;
public class ParticleCollide : MonoBehaviour { public ParticleSystem ps; GameObject GC;
void Start () {
GC = GameObject.Find ("GrabberCube");
ps = GetComponent<ParticleSystem>();
}
void OnParticleCollision(GameObject other){
ParticleSystem.Particle[] particles = new ParticleSystem.Particle[ps.particleCount];
int num = ps.GetParticles (particles);
//Debug.Log("found "+num+"of particles");
for (int i=0; i<num; i++) {
//Debug.Log ("position " + particles [i].position.z);
**if (particles [i].position.z == GC.transform.position.z)**
particles [i].velocity -= Vector3.up * -7.5f;
particles [i].lifetime = 0.2f;
Debug.Log ("Particle grabbed.");
}
ps.SetParticles(particles,num);
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612000909im_/https://answers.unity.com/themes/thub/images/avi.jpg)