- Home /
particle array null refrence
I'm just messing around trying to add some external effects to my particles(particle attraction in this case). but it seems my particle array isn't being filled, or something is going wrong when i try to fill it with my particleSystem's getParticles() function. i must be creating the array wrong or trying to fill it incorrectly. can anyone see my problem?
#pragma strict
var target : Transform;
var power = 5.0;
var PA: ParticleSystem.Particle[];
var heading:Vector3;
function Start ()
{
}
function Update ()
{
particleSystem.GetParticles(PA);
for(var p in PA)
{
heading = target.position - p.position;
p.velocity += heading.normalized * power;
}
particleSystem.SetParticles(PA, PA.Length);
}
error is at the 'for' line:
NullReferenceException: Object reference not set to an instance of an object
i also tried this for loop, but got the same error:
for(var i = 0; i < PA.Length; i++)
{
heading = target.position - PA[i].position;
PA[i].velocity += heading.normalized * power;
}
Are you attach this script at GameObject with your ParticleSystem?
Try caching your particle system and adding it manually from the editor.
Answer by zharik86 · Dec 08, 2014 at 10:17 AM
I think, you dont't using prewarm options for your particle. Than, in 1st frame count of your particle, probably, is 0. Than for 1st frame array PA is null. Than:
#pragma strict
var target : Transform;
var power = 5.0;
//Initialization your array
var PA: ParticleSystem.Particle[] = new ParticleSystem.Particle[1000];
var heading:Vector3;
function Start () {
}
function Update () {
var myLen: int = particleSystem.GetParticles(PA);
if (PA != null) {
for(var i: int = 0; i < myLen; i++) {
heading = target.position - PA[i].position;
PA[i].velocity += heading.normalized * power;
}
particleSystem.SetParticles(PA, myLen);
}
}
Of course, you use GetParticle in "if": return number of particle. I hope that it will help you.
oh yeah thanks, i forgot to handle the exceptions. using this code i got rid of the exception:
particleSystem.GetParticles(PA);
if(PA != null)
{
for(var p in PA)
{
heading = target.position - p.position;
p.velocity += heading.normalized * power;
Debug.Log("Working");
}
particleSystem.SetParticles(PA, PA.Length);
}
But still doesn't run the 'debug' line. which means my array still isnt filling with particles.
sorry i was away from my computer today, but thank you this works perfectly now. :)
Your answer
Follow this Question
Related Questions
NullReferenceException weird error 1 Answer
Writing to txt file using textfield problem 2 Answers
Accessing individual particles in array? 1 Answer
Error putting an object into an Array 0 Answers