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;
}
}
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;
}
}
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;
}
}
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
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
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.
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.