I’m currently working in a game and got a problem with the information exchange between my scripts. That’s what i have:
Script A calculates quite much and attached to a emty GameObject.
Script B is needed for the movement behaviors of my monsters. Sripts B is attached to each monster in the scene. Script B needs a public variable of Script A. How can i get this information? Script A has get-methods/properties for this, but i don’t know how i can call them? Maybe somehow with get_component?
PS: I don’t want script B to inherit script A, because script A should only be running once.
Thanks for your help
Using static variables is ok in some cases but it can cause subtle problems in complex programs. Instead you can add a public variable of type
ScriptA into your
ScriptB and either assign it via the inspector or using any find function (
FindObjectsOfType( typeof( ScriptA ) ) as ScriptA,
Find( "ScriptAObjectName" ).GetComponent< ScriptA >(), etc.) if your ScriptB objects are spawned dynamically. Then you can access any public properties, variables, etc. from your
declare a static variable (or function) in Script A
public static var someStaticVar
you can call that variable from any other script (like Script B) by calling