Hi, I have particle class which needs some parameters from my main behaviour class. These parameters should be changed in the editor in real time. I am wondering what is the best way of passing them to the class.
I’ve thought of 4 possible ways:
- Declaring them as static
- Including them as an attribute of each particle (not a good idea)
- Passing a parent class to each particle and accesing the global parameters from the parent class
- Passing them as arguments to the function that needs them
The first method would not let me update the variable in the inspector. The second one doesn’t make much sense. I think the 3rd method makes sense, but I haven’t seen this kind of things in Unity. The fourth is ok, but I should pass a lot of parameters, so I wonder if there is a better option…
using UnityEngine;
using System.Collections;
public class ParticleScript : MonoBehaviour {
// A series of global parameters
public float p1 = 1f;
public float p2 = 2f;
// They can be a lot of parameters actually...
// *1* I could make them static, but I dont really
// need them to be accesible from anywhere else than the Particle class
void Update () {
Particle p = new Particle ();
p.MethodThatNeedsParameters ();
}
}
public class Particle{
// *2* Including them as attributes in each particles makes no conceptual sense.
// And I should use an CustomEditor if I want to update them
// *3* I could pass the parent class as a parameter like
// ParticleScript parent;
// And then pick the parameters from there (I am not sure if they would be updated)
public void MethodThatNeedsParameters(){
// *4* Passing them as arguments is an option, but they are many
// Do something with the parameters
}
}