- Home /
how to access Shuriken Particle System Modules in script
hi guys, i want to access the new particle system modules such as "emission" and "shape" in script, so that i can change the particle style during runtime . but i can not find any api to access the modules after i get the ParticleSystem component. is there a way to access the modules using script ? how ? thanks a lot !
Answer by flyingbanana · Apr 17, 2015 at 11:34 PM
You can't by normal means, however, with the aid of legacy animations, you can.
First, add the Animation component (not Animator!) to your object you want to animate.
Then in code, each time you want to animate a property, use this snippet:
Animation animation = GetComponent<Animation>();
AnimationCurve curve = AnimationCurve.Linear(startTime, startValue, endTime, endValue);
AnimationClip clip = new AnimationClip();
clip.legacy = true;
clip.SetCurve("", typeof(ParticleSystem), "ShapeModule.radius", curve);
animation.AddClip(clip, "Resize");
animation.Play("Resize");
The above code will animate the radius property of the shape module from start value to end value. In general, the property key is named "nameModule.property", but if you want to know for sure, do the following:
Under Edit > Project Settings > Editor, change Asset Serialization mode to Force Text
Look for the scene containing your particle effect. Open in text editor.
Search for the particle system and look for the key you want to change.
Of course, for an instantaneous change, just define the animation curve to be something like
AnimationCurve curve = AnimationCurve.Linear(0, 0, 0, newValue);
Took me a while to get it working! :)
Answer by Bocharick · Feb 10, 2016 at 09:00 PM
I found solution of changing shape through script.
NOT working:
public ParticleSystem psystem;
void Start(){
psystem.shape.box = new Vector3(2,2,2); // error, couldn't compile
//psystem.shape.box.Set(2,2,2); // has no error, and compile, but has no results
}
Working:
public ParticleSystem psystem;
void Start(){
var x = psystem.shape;
x.box = new Vector3(2,2,2); // solve my problem
//x.box.Set(2,2,2); - still has no error, but still has no results
// if you want change shape cone radius - x.radius = 34345;
}
Hope it will helpfull
Your Not Working snippet didnt work because you didnt use GetComponent on the Particle System, I reckon.
show me your "Getcomponent" variant plz, cause i don't understand it.
Basically, when you define your variable
public ParticleSystem psystem;
its an empty instance (usually).
void Start(){
psystem = GetComponent<ParticleSystem>();
The GetComponent retrieves the object's instance and now your system is a reference to it..
This works and is the best solution yet. Thanks for sharing!
Answer by WILEz1975 · Sep 05, 2014 at 02:10 PM
You can modify all variables.
For example, i change my angle shape:
SerializedObject so = new SerializedObject(GetComponent<ParticleSystem>());
so.FindProperty("ShapeModule.angle").floatValue = 25f;
so.ApplyModifiedProperties();
You can see all particleSystem propriety with:
SerializedProperty it = so.GetIterator();
while (it.Next(true))
Debug.Log (it.propertyPath);
(sorry for my bad english, i'm italian).
"SerializedObject" only seems available only with UnityEditor namespace, but even when I add that, the Apply$$anonymous$$odifiedProperties does not affect the component. So I don't think this solution works for OP too:
so that i can change the particle style during runtime
Answer by CodeMasterMike · Jan 14, 2013 at 11:36 AM
Read the documentation on what variables and functions you can access with the Shuriken particle system, because you can't change "shape" through scripts. You can only change those in the editor.
Shuriken particle system documentation
Good luck!
Sorry for being that bold, but not having full access to shurikan modules from code is like half baked cake.
imagine that a project has hundreds of prefabs which all needs a mesh based particles, the only way of having it now is by duplicating. now, if it need some tuning, it's a pain and not fun going on all duplications again.
This must be added ASAP.
I converted your answer to a comment because the answer are really just used to answer the main question.
Answer by rawrrrz · Feb 29, 2016 at 12:07 PM
Old question, but I just came across this trying to google for myself. To explain what I was doing a bit, in a 'diablo-ish' kind of game, I have a spell explode, and attach an 'I'm on fire!!' effect object.. By default, it's just a lame little sphere as it's default emission shape, so depending on what it hit I then wanted to change it from emitting my sphere to SkinnedMeshRenderer, MeshRenderer, etc..
Firstly, I declared a GameObject parentObject; and set it with my OnCollision script, as my meshes are always buried in child objects of the monster's parent containing my collider.
//Get particleSystem's ShapeModule
ParticleSystem particleSystem = GetComponent<ParticleSystem>();
ParticleSystem.ShapeModule shapeModule = particleSystem.shape;
//Check&Set for MeshRenderer, set Shape MODULE's ParticleSystemShapeType
if (parentObject.GetComponentInChildren<SkinnedMeshRenderer>())
{
shapeModule.shapeType = ParticleSystemShapeType.SkinnedMeshRenderer;
shapeModule.skinnedMeshRenderer = parentObject.GetComponentInChildren<SkinnedMeshRenderer>();
}
Just a snippet, I change more variables, like meshShapeType (Vert/Edge/Tri), which I actually found before just shapeType, but it gave me hope! lol, and useMeshColors, etc.., and else if through the other types, then if none found, it'll just stay it'll silly default sphere emitter.. and it works like a charm in my project's latest build!
Really took some digging to find "shapeType". I kept trying to change .shape assuming it was the Shape Property and not the Shape Module.. Finally IntelliSense said 'MODULE', and I recalled the docs.. http://docs.unity3d.com/Manual/ParticleSystemModules.html .. So, that's why I purposely declared particleSystem and shapeModule in that way, to illustrate what about this made me feel the most stupid and caused me to google lol! :D
Also may need to note that I'm on 5.3.2p2.. I'm a few patches behind now, but they've been working a lot on the Particle System recently, so this may still be fairly new.. Anyways, hope this helps someone access their variables, in case I wasn't the last person still trying. :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613090920im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Set a range of startSpeed for Shuriken via script 2 Answers
Shuriken Scripting, disappointed 1 Answer
Change new Shuriken Particle System mesh/texture in script? 0 Answers
shuriken auto destruct script doesn't ALWAYS work? 2 Answers
Have null errors 1 Answer