- Home /
How can I increase Health Value on trigger enter, to another game object?
I'm new to coding so I'm wasting hours on end trying to figure this out.
I want to add +1 Health to other gameobject (which has Health script attached and controls gameover etc) when the Player gameobject collides with a coin/collectable.
Can somebody help?
This is what I have so far:
using UnityEngine; using System.Collections;
public class SendHealthToPlayer : MonoBehaviour {
 public int healthValue;
 private Health triggerforattackbounds;
 void Start ()
 {
     GameObject gameControllerObject = GameObject.FindGameObjectWithTag ("Player");
     if (gameControllerObject != null)
     {
         triggerforattackbounds = gameObject.GetComponent <Health>();
     }
     if (triggerforattackbounds == null)
     {
         Debug.Log ("Cannot find 'Triggerforattackbounds' script");
     }
 }
 void OnTriggerEnter (Collider other)
 {
     if (other.gameObject.tag == "Player")
     {
         Health triggerforattackbounds = other.GetComponent<healthValue>.currentHealth += 1();
     }
 }
}
Answer by robertbu · Aug 13, 2014 at 01:15 AM
You are pretty close. Line 22 should be:
   other.GetComponent<Health>().currentHealth += 1;
This assumes that the name of the script for health on the player is 'Health' and that the variable that stores the health is 'currentHealth'. I cannot tell what the name you use for the script that has 'currentHealth' in it. Note the first formatted line can be deleted.
Answer by zee_ola05 · Aug 13, 2014 at 01:16 AM
This
 Health triggerforattackbounds = other.GetComponent<healthValue>.currentHealth += 1();
should be
 Health triggerforattackbounds = other.GetComponent<healthValue>().currentHealth += 1;
Answer by patrick-holder · Aug 13, 2014 at 10:29 AM
Thanks for your answers but this give the Playermodel that is controlled +1 Health but not the gameobject called "triggerforattackbounds" which controls gameover etc... They are both tagged "Player", is this the wrong way of doing it or can I give both objects +1 Health?
This is the Health Script on "triggerforattackbounds" which I added bit to:
public class Health : MonoBehaviour { public AudioClip GameOver; public AudioClip impactSound; 
 public AudioClip hurtSound; 
 public AudioClip deadSound; 
 public int currentHealth = 3; 
 public bool takeImpactDmg; 
 public bool onlyRigidbodyImpact; 
 public bool respawn; 
 public string[] impactFilterTag; 
 public float hitFlashDelay = 0.1f; 
 public float flashDuration = 0.9f; 
 public Color hitFlashColor = Color.red; 
 public Transform flashObject; 
 public GameObject[] spawnOnDeath; 
 public PauseManager pauseManager;
 string message = string.Empty;
 
 [HideInInspector]
 public bool dead, flashing;
 [HideInInspector]
 public Vector3 respawnPos;
 
 private Color originalColor;
 private int defHealth, h, hitForce;
 private bool hitColor = false;
 private float nextFlash, stopFlashTime;
 private Throwing throwing;
 
 //setup
 void Awake()
 {
     if(currentHealth <= 0)
         Debug.LogWarning(transform.name + " has 'currentHealth' set to 0 or less in 'Health' script: it has died upon scene start");
     audio.playOnAwake = false;
     if(flashObject == null)
         flashObject = transform;
     originalColor = flashObject.renderer.material.color;
     defHealth = currentHealth;
     respawnPos = transform.position;
     obj.Volume = 0.4f;
 }
 
 //detecting damage and dying
 void Update()
 {        
     //flash if we took damage
     if (currentHealth < h)
     {
         flashing = true;
         stopFlashTime = Time.time + flashDuration;
         if (hurtSound)
             AudioSource.PlayClipAtPoint(hurtSound, transform.position);
     }
     h = currentHealth;
     
     //flashing
     if (flashing)
     {
         Flash ();
         if (Time.time > stopFlashTime)
         {
             flashObject.renderer.material.color = originalColor;
             flashing = false;
         }
     }
     
     //are we dead?
     dead = (currentHealth <= 0) ? true : false;
     if (dead)
     {
         ResultManager.Instance.ShowResultBox(true,Random.Range(1,4),new CallbackFunction[]{DoReplay});
         AudioSource.PlayClipAtPoint(GameOver, Camera.main.transform.position);
         Time.timeScale = 0;
         obj.Volume = 0.0f;
         Destroy(pauseManager);
     }
 }
 void DoReplay()
 {
     message = "Click replay button";
 }
 
 //toggle the flashObject material tint color
 void Flash()
 {
     flashObject.renderer.material.color = (hitColor) ? hitFlashColor : originalColor;
     if(Time.time > nextFlash)
     {
         hitColor = !hitColor;
         nextFlash = Time.time + hitFlashDelay;
     }
 }
 
 //respawn object, or destroy it and create the SpawnOnDeath objects
 void Death()
 {
     //player drop item
     if(tag == "Player")
         throwing = GetComponent<Throwing>();
     if(throwing && throwing.heldObj && throwing.heldObj.tag == "Pickup")
         throwing.ThrowPickup();
     
     if (deadSound)
         AudioSource.PlayClipAtPoint(deadSound, transform.position);
     flashing = false;
     flashObject.renderer.material.color = originalColor;
     if(respawn)
     {
         if(rigidbody)
             rigidbody.velocity *= 0;
         transform.position = respawnPos;
         dead = false;
         currentHealth = defHealth;
     }
     else
         Destroy (gameObject);
     
     if (spawnOnDeath.Length != 0)
         foreach(GameObject obj in spawnOnDeath)
             Instantiate(obj, transform.position, Quaternion.Euler(Vector3.zero));
 }
 
 //calculate impact damage on collision
 void OnCollisionEnter(Collision col)
 {
     if(!audio.isPlaying && impactSound)
     {
         audio.clip = impactSound;
         audio.volume = col.relativeVelocity.magnitude/30;
         audio.Play();
     }
     
     //make sure we take impact damage from this object
     if (!takeImpactDmg)
         return;
     foreach(string tag in impactFilterTag)            
         if(col.transform.tag == tag)
             return;
     if(onlyRigidbodyImpact && !col.rigidbody)
         return;
     
     //calculate damage
     if(col.rigidbody)
         hitForce = (int)(col.rigidbody.velocity.magnitude/4 * col.rigidbody.mass);
     else
         hitForce = (int)col.relativeVelocity.magnitude/6;
     currentHealth -= hitForce;
     //print (transform.name + " took: " + hitForce + " dmg in collision with " + col.transform.name);
 }
}
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                