- Home /
How to add a variable to a prebuilt class?
Hey,
Im trying to make a temp for my particle system and the way im trying is to add a variable to the particlesystem class. If it isn't possible to edit the class how would I add the temp variable to every particle in the system and access it to change it? This is the script I got right now:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ParticleControl : MonoBehaviour {
public float RoomTemp = 22;
ParticleSystem System;
ParticleSystem.Particle[] Particles;
private void Update()
{
foreach(ParticleSystem.Particle num in Particles)
{
}
}
}
If anyone can help me ill appreciate it.
Answer by Nischo · Oct 19, 2017 at 06:57 PM
Obviously you can't add a new variable to a compiled class, but you can use customData for storing data for every particle.
Well I need to store a float but the custom data only supports color and vector and I also tried to make a script to make it store a float but I couldn't. So there must be another way right?
Its color and vector because that is what a vertexshader can handle perfectly, and there is no float, because a color and a vector are 3(4) floats. ;) But i think you can't use arbitrary values in customData only curves. You can always just store values alongside the particles, but then you have to react to new and destroyed particles. Remember that the whole system is built for speed, updating it via script is already rather slow.
I thought of this, I have a list of particles and a list of temps and use the same index for them so like index 5 would be water and on the temp list index 5 would be 22 C. Would this work without having a major performance impact and how would I make this?