- Home /
Accessing Shuriken particle system values through a script in C#
Hello, I have massive problems accessing a particle system through a script.
What I want: a particle system that spawns particles continuously which then go in a line to the right. When dragging a GUI slider the speed or the size of the particles should change.
What I have: GUI slider is no problem,I also managed to create the particle system in the scene and it looks fine with the settings I gave it in the instructor. I called this particle system 'Spawner'. I also created a C# script called 'mosfetlogic' which I use to show the GUI slider and which should access the particle system. But this is where I get stuck and I cannot seem to find any useful tutorial/example to access it.
I do not need to access each particle on its own, just the basic values that I can change in the instructor. Right now I am really confused from all the different examples that I tried and didn't work so I don't even know where to start with the particle system.
Please help me if you have any clue, I am going crazy over this.
This is the code I have now, I want to use val to change values in the particle system:
using UnityEngine;
using System.Collections;
public class mosfetlogic : MonoBehaviour {
private float val, tVal, bVal;
private Rect rect = new Rect(25, 25, 10, 300);
// Use this for initialization
void Start () {
val = 1.0F;
tVal = 3.0F;
bVal = 0F;
}
void OnGUI(){
val = GUI.VerticalSlider(rect, val, tVal, bVal);
}
}
Answer by Berenger · Jun 08, 2012 at 03:22 PM
You need access to the particle System component. There is a property particleSystem (does it works ?) or GetComponent. Then, here are all the var you can access.
Epifistu : Thank you very much but the problem is that I don't know how to access it. All I need would be an example code on how to change one of the values :S
$$anonymous$$e : Sure sure. The following script must be attached on the same GameObject than the particle system
ParticleSystem ps;
private void Start(){
ps = GetComponent< ParticleSystem >();
}
private void OnGUI(){
ps.emissionRate = GUI.VerticalSlider( ps.emissionRate, 0, 100 );
}
This is wonderful, thank you very much it's exactly what I was looking for! :D
Answer by Epifistu · Jun 08, 2012 at 04:14 PM
Thank you very much but the problem is that I don't know how to access it. All I need would be an example code on how to change one of the values :S
Answer by coding_crow · Oct 06, 2014 at 01:16 PM
In any script that is attached to the same GameObject as your shuriken, you can call transform.particleSystem for a shortcut. Then You can use particleSystem.GetParticles() and particleSystem.SetParticles(). Be warned though that the latter WILL cause a bug where your Shuriken will only render when its emitter is in view of the camera, thus causing individual particles to be culled while still in view.
class MyClass : MonoBehaviour{
ParticlesSystem shuriken;
void Start()
{
shuriken = particleSystem;
SetParticlesToFormALine();
}
void SetParticlesToFormALine()
{
ParticleSystem.Particle[] particles = new ParticleSystem.Particle[8];
for(int particleCount = 0; particleCount < 8; particleCount ++)
{
particles[particleCount ] = new ParticleSystem.Particle();
particles[particleCount ].position = Vector3.right*i;
particles[particleCount ].color = Color.White;
particles[particleCount ].size = 1f;
}
shuriken.SetParticles(particles,particleCount);
}
}
a very good tutorial on scripting shuriken: http://catlikecoding.com/unity/tutorials/graphs/
the culling bug: http://answers.unity3d.com/questions/218369/shuriken-particle-system-not-rendering-particles-w.html
Your answer
Follow this Question
Related Questions
Scripting Shuriken 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
why is particle system duration read only? 2 Answers
How do I create a Shuriken moving particle system? 0 Answers
Changing sub emitter via script? 1 Answer