can anyone tell me why this code will not work

i have a scene with a sphere and a basic particle system just generating 5 basic particles,

i have the code below atached to the sphere and the particle system dragged into the reference box on the script on the sphere

i am trying to get an array of the particles alive and then choose one at random then move the sphere to that position, i have tried for a while and searched for answers, i have tried using a list but it just kept giving errors. i just can not get why this is not working

thank you

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class GETPARTICLE5 : MonoBehaviour
{
public ParticleSystem ps;
ParticleSystem.Particle[] particles;
private int index;
private Transform MoveTo;


void  Start (){
     ps = GetComponent<ParticleSystem>();
}

void update(){

    index = Random.Range (0, particles.Length);
    var MoveTo = particles[index];

     transform.position = MoveTo.position;
}
}

It’s Update(), not update()…

lol dont know how i missed that, but even after that i drag the particle system to the slot on the script in the inspector on the sphere object, then when i run it the slot just says none and the get a red error of no reference to the object, then when i stop it running the slot shows that the particle system is in the slot

is this line wrong

ps = GetComponent();

is it looking for something that is not in the particle system, it makes no sense that this is not working, why is it not referencing the particle system when in the slot on the script

If you are dragging and dropping the particle system onto the script in the project view, you don’t need that line in start. Does the object with the script on have a particle system? If not, it will throw up an error. Before you start the script, the particle system is linked correctly, but once the script starts, it searches for a particle system that is on the same object as the script, and if there is none it will throw up a null.

It’s also interesting that you don’t ever seem to use your ps particle system. Is your particles array meant to be an array of particles in ps? You don’t seem to fill the particles variable with any information.

code so far:, i put this code on the sphere object, in the inspector i drag the object in the hierarchy with the particle system on it to the slot on the sphere asking for the particle system,

when i run it i get an error, object reference not set to an instance of an object,

what i am trying to do is create an array of the particles currently emitted, choos one at random from the array, then move the sphere to the particles position, this sounds so stupidly simple but it is causing me no end of trouble, have i got the array in the right place above start?

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class GETPARTICLE7 : MonoBehaviour
{
public ParticleSystem ps;
ParticleSystem.Particle[] particles;
private int index;
private Transform MoveTo;


void  Start (){
//    ps = GetComponent<ParticleSystem>();
}

void Update(){

    index = Random.Range (0, particles.Length);
    var MoveTo = particles[index];

     transform.position = MoveTo.position;
}
}

How do you manage to make that kind of mistake (update() instead of Update()) now that Visual Studio format everything for you nowadays?

Type

void upd

click Tab or Enter and Visual Studio will do the rest and display the complete -and correct- lines:

private void Update()
{

}

dont know, i didnt notice it and unity never gave a syntax error?, but any way that was not the problem, i have now changed the code to GetParticles as below but still getting a null reference error which is strange as the particle system is referenced in the script slot on the inspector

now in the random.range i can not use .length as it gives an error saying int can not use length
so how the hell do i get the length of the particle array, looks like this is becoming impossible for unity to do this

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class GETPARTICLE8 : MonoBehaviour
{
public ParticleSystem ps;
ParticleSystem.Particle[] particles;
private int index;
private Transform MoveTo;


void  Start (){
 
//    ps = ps.GetComponent<ParticleSystem>();
}

void Update(){

    index = Random.Range (0, ps.GetParticles(particles));
    var MoveTo = particles[index];

     transform.position = MoveTo.position;
}
}

Try this line in your void start

particles = new ParticleSystem.Particle[ps.main.maxParticles];

the line gets rid of the null reference error but the sphere still will not move to a particle position, is there a way to create the array in the update so it is finding the new particles that are created a bit like using find with tag or name

think it is time to give up on this as it seems unity cant even do this

Accusing the tool is always a good reason for giving up.

sorry but unity has no way of creating a constant running array in update and will not get the position of live particles, an array will only be created before start, as particles get destroyed i would need the array to constantly update with new created particles, and even if this was possible would take alot of fps which i dont need, so i will just use a random positioning of my game object, to be honest it looks just as good i imagine, i have come to unity from unreal 4 engine to see if it is better being modular, but there are unity limits compared to ue4

There are hardly any limits to anything but your understanding of the system. But whatever. I guess you have every right to be angry at yourself.

2 Likes

as i said, trying to get an array of particle to update would use a considerable amount of cpu to basically do what i have now done just by using random position, and yes compared to ue4, unity does have some limitations but is a lot easier to work with so both have their advantages

Hi, Could you describe again, with other words, what you wanted to do? I’m interested in trying something of my own but I don’t really understand what you were trying to achieve. :slight_smile:

Hi, i have a scene with a particle system, the particle system lets out say 10 particles at a time, i have a sphere in the scene, i wanted to have a script on the sphere that referenced the particle system and created an array of the particles, then moved the sphere to a random particle position from the array, now as the particles get destroyed this means that the array must keep being filled with the new particles but i could not get the array to run in update , i could only create the array before start as it kept giving red errors. thanks

Sorry for the late answer. This is an interesting problem, especially the part where the particles need to stay stored in the array after disappearing. I haven’t had much time to try anything yet but I will.

In fact, what you need to capture is the position of the particle at some point before it disappears, not the particle itself. Did you try a raycast that would store that value after hitting a particle?

I had thought of using a raycast but i decided against it as it would use more resources than just placing at a random point in the area of the particle and to be honest you could not tell the differnce in the end, but i still would like to know if there was an alternative way of doing it by capturing the particle position, but how do you fill the array constantly with the new born particles when the array is initiated at the begining of the code and can not be run in update.

Actually, this is the main problem. I don’t know whom to tag to draw their attention though. @ maybe?

I need to point out that this isn’t true. Particle systems are optimized for doing exactly this sort of thing. Try it in update and see.
As for figuring out how big your particle array needs to be, you should read the docs before saying something is impossible. There are at least two different ways to do this. You can either look at the particle systems particle count and allocate an array of that size (https://docs.unity3d.com/ScriptReference/ParticleSystem-particleCount.html) or you can pass a null array in, and GetParticles will allocate it automatically (https://docs.unity3d.com/ScriptReference/ParticleSystem.GetParticles.html). The example code also shows the opposite case, using a preallocated buffer without making a new array every frame. If you want to know how big to make the array in Start (or if you use lazy allocation in Update), you can check the particle system’s main module’s max particle count (https://docs.unity3d.com/ScriptReference/ParticleSystem-main.html, https://docs.unity3d.com/ScriptReference/ParticleSystem.MainModule-maxParticles.html).