- Home /
Changing a Particle System shape values at runtime.
So, I have a couple particle systems in my scene with "Shape" Activated and "Box" selected. I'd like to modify the value of the "Box X" field at runtime.
Specifically, I'm trying to have the particle system match the width of the screen once it starts.
Here's what I tried, where the floats leftXpos and rightXpos are the screen edges.
ParticleSystem ps = go.GetComponent<ParticleSystem> ().shape.box = new Vector3(rightXpos - leftXpos, 1f,1f);
and the errors I get...
Assets/_Scripts/Managers/PlaneManager.cs(38,80): error CS1612: Cannot modify a value type return value of 'UnityEngine.ParticleSystem.shape'. Consider storing the value in a temporary variable
Assets/_Scripts/Managers/PlaneManager.cs(38,80): error CS0200: Property or indexer 'UnityEngine.ParticleSystem.shape' cannot be assigned to (it is read only)
save shape in local var and then apply changes.
using UnityEngine;
public class ParticleTest : $$anonymous$$onoBehaviour {
private GameObject go;
float leftXpos = 0;// whatever you have like (Screen Edges)
float rightXpos = 0; whatever you have like (Screen Edges)
void Start ()
{
var ps = go.GetComponent<ParticleSystem>().shape.box;
ps = new Vector3(rightXpos - leftXpos, 1f, 1f);
}
}
Answer by herDev · Apr 26, 2017 at 01:07 PM
Hi there, you can set it like this:
ParticleSystem particleSystem;
public ParticleSystemShapeType boxShape = ParticleSystemShapeType.Box;
public Vector3 boxSize = new Vector3(100.0f, 1.0f, 1.0f);
void Start()
{
particleSystem = this.gameObject.GetComponent<ParticleSystem>();
var shape = particleSystem.shape;
shape.shapeType = boxShape;
shape.box = boxSize;
}
You'll still need to calculate the correct value for X but at least it will set it correctly now. Hope that helps!
Answer by Potter-Solutions · Apr 28, 2017 at 05:42 AM
You can create new objects for the Shape. Set it equal to the ParticleSystem.Shape and then you can modify your copy. Because the copy points to the original Shape, you are actually modifying the original values.
You can do a similar trick to modify the Box. Since this is a Vector3, just create a new Vector3 and make the original Box equal to the new Vector3.
UnityEngine.ParticleSystem.ShapeModule editableShape = myParticleSystem.shape;
Vector3 newBox = new Vector3 (1f, 1f, 1f);
editableShape.box = newBox;
I don't know why this works nor do I know why Unity won't let you edit the property of the shape directly.