How to make a reference to an other script variable using variable's name

Hello,

I wonder how it’s possible to write in a script a reference to an other script using variables.

I explain. For example:

private MyBehavior myBehavior;
public Float storeTestFloat;

public override void OnUpdate()
		{
storeTestFloat.Value = myBehavior.testFloat;
}

With this example. We are accessing the variable “testFloat” from the script names MyBehavior.

Now how I can do if I want to set my script name and his variable name using variables?

storeTestFloat.Value = MyVariableScriptName.MyFloatVariableName;

So with this, I will be able to use this script with variables instead of making multiples.

I know, my questions are most of the time not simple, but I know some of you are very talented, and I’m sure some of you can enlighten me :slight_smile:

Thanks people!

Since you already know the gameObject you want to reference and the script name in that gameObject, I think you could just do DudeGameObject.GetComponent(MyVariableScriptName) and then follow the 2nd link i posted. Something like:

Component  attachedScript = DudeGameObject.GetComponent(MyVariableScriptName);
int myValue = (int)( attachedScript.GetType().GetField(MyFloatVariableName).GetValue(attachedScript);)

Shouldn’t this work

public “NAMEOFSCRIPT” “Name whatever you want it to be”

So for example

public PlayerHealth pHealth;

Then in your Update or whatever can can do something like,

pHealth.Die = true;

^^ That’s how to access a bool.

But everything has to be in PUBLIC to access.
But aside from that, all you have to do is drag and drop the script/Object containing the script into the pHealth slot.