Hi,
I have a public float in one of my scripts that I need in another one of my scripts, I have used GetComponent to reference the script where the public float is, I just don’t know how to get this script to see the float. Can anyone help?
Paps_O
2
Well if I understand correctly, here an example:
- attach each script to a gameobject in the hierachy
- then select the gameobject that has the scriptA
- and drag and drop it to the field in the scriptB (in the inspector)
Note: to do it: first select the scriptB, then click without releasing the scriptA (that way the scriptB will stay open on the inspector)
public class ScriptA : MonoBehaviour
{
public float myPublicFloat;
}
public class ScriptB : MonoBehaviour
{
public ScriptA attachedScriptA;
// Update is called once per frame
void Update()
{
float damage = attachedScriptA.myPublicFloat * 9000;
Debug.Log("Hit something with damage: " + damage);
}
}