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
	}


}

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);
     }
 }