- Home /
Best way of passing a parameter to a class
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: 1. Declaring them as static 2. Including them as an attribute of each particle (not a good idea) 3. Passing a parent class to each particle and accesing the global parameters from the parent class 4. 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
}
}
not sure if particles transforms, but if they do you can do particle.transform.SetParent(parent game object name.transform); something along those lines
use static reference of ParticleScript in particle class and assign it once. it will allow you to access global var of ParticleScript and you dont have to pass reference of class too, also vars are visible in inspector.
@Vice_Versa, as it is a custom particle class, it does not have a transform.
Answer by fafase · Jun 26, 2015 at 06:36 AM
My advice on it.
First off, I see one wrong consideration in your code:
// *1* I could make them static, but I dont really
// need them to be accesible from anywhere else
Static does not mean access, you could actually make private static and then no access outside the class.
But now let's try.
You claim to have a lot of data to pass. Instead of having each data (p1, p2,...) as member of ParticleScript, I would create a new class in which those would be:
public class ParticleScript:monoBehaviour{
public DataStorage data;
}
[System.Serializable] // This is important to display class in inspector
public class DataStorage{
public float p1, p2;
}
Now you can pass your data object:
void Update () {
Particle p = new Particle ();
p.MethodThatNeedsParameters (data);
}
public class Particle{
public void MethodThatNeedsParameters(DataStorage data){
Debug.Log(data.p1);
Debug.Log(data.p2);
}
}
That is a very good advice @fafase. What about passing a ParticleScript reference to the Particle class? Are there any drawbacks about this approach?
Your answer
Follow this Question
Related Questions
Caching global vareiables? (n00b question) 1 Answer
[SOLVED] Global Variable in C# class seems to be acting like a local variable. 1 Answer
class design preference 1 Answer
Inheritance from object to object 1 Answer
Where can I find Unity Script Tutorials that teach how to make the scripts come together? 2 Answers