- Home /
Sending a variable to a script, then returning it to the previous script. C#
I'm currently working on enemy design for a game and I need to get it to damage the player. I need to send the variable from the player health script, to the enemy script, have the enemy script alter the health value and then send it back to the health script. The problem is, I have no real clue how to do this.
Very basic health script (Ignore the commented out parts, that's something for another question.)
public class PlayerHealth_Test : MonoBehaviour {
public static int CurrentHealth = 100;
//void Update () {
// if (CurrentHealth == 0)
// {
//Destroy(GameObject(Player.Object));
// }
//
//}
}
And the enemy script:
public class Leech_AI_Test : MonoBehaviour {
public float MaxDistance = 10.0f;
public float MoveSpeed = 5.0f;
public float MinDistance = 0.0f;
public Transform Target;
public float LookAtDistance = 10.0f;
public float Distance;
public float Damping = 5.0f;
private Quaternion rotation;
public bool parenting = false;
public float curHealth;
void Start (){
Target = GameObject.FindGameObjectWithTag ("Player").transform;
parenting = false;
// curHealth = gameObject.GetComponent<PlayerHealth_Test>().CurrentHealth;
}
void OnTriggerEnter(Collider Player)
{
transform.parent = GameObject.FindGameObjectWithTag("Player").transform;
parenting = true;
}
void Update () {
Distance = Vector3.Distance (Target.position + Target.transform.forward, transform.position);
if (Distance < LookAtDistance)
{
LookAt();
}
if (Distance > MinDistance && Distance < MaxDistance && parenting == false)
{
follow ();
}
//Debug.Log("curHealth", curHealth);
}
void FixedUpdate ()
{
if (parenting == true)
{
curHealth -= 0.05f;
}
}
void LookAt(){
rotation = Quaternion.LookRotation (Target.localPosition - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}
void follow(){
transform.Translate(Vector3.forward * MoveSpeed * Time.deltaTime);
}
}
I need to send the currenthealth value from the playerhealth script to the leech script, allow the leech script to damage the player, and send that value back to the playerhealth script, and that needs to happen continuously until the leech is dealt with.
Any information on how to do this, or a better way to do this (since this seems pretty unrefined to me) would be appreciated.
This tutorial here could help:
Answer by aldonaletto · Nov 03, 2013 at 10:38 PM
Just get a reference to the PlayerHealth_Test script and modify CurrentHealth directly. If this script is attached to the player, use Target to get the script:
...
public bool parenting = false;
public float curHealth;
private PlayerHealth_Test playerHealth; // declare this variable
void Start (){
Target = GameObject.FindGameObjectWithTag("Player").transform;
// get a reference to PlayerHealth_Test that's attached to the player:
playerHealth = Target.GetComponent<PlayerHealth_Test>();
parenting = false;
}
...
void FixedUpdate (){
if (parenting){
// modify CurrentHealth directly in the target script:
playerHealth.CurrentHealth -= 0.05f;
}
}
...
Your answer
![](https://koobas.hobune.stream/wayback/20220613121204im_/https://answers.unity.com/themes/thub/images/avi.jpg)