- Home /
Heath regain via Pickup code halp me
So I am trying to develop a pickup script but nothing I do seems to be working. I get a boatload of errors and everything I do to fix it seems to have that close but no cigar thing going for it. the code is in c# and it uses two scripts to execute one to manage player heath and the other exists to destroy the game object depending on whether or not the player is at full health. Note that 90% of this code is reverse engineered from various parts of the survival shooter unity project.
I beleive the second code is bug free
first code named PlayerHealth
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerHealth : MonoBehaviour
{
public int startingHealth = 100;
public int currentHealth;
public Slider healthSlider;
public Image damageImage;
public AudioClip deathClip;
public float flashSpeed = 5f;
public Color flashColour = new Color(1f, 0f, 0f, 0.1f);
AudioSource playerAudio;
PlayerShooting playerShooting;
bool isDead;
bool damaged;
bool AtFullHealth;
bool Regain;
CapsuleCollider CapsuleCollider;
void Awake ()
{
playerAudio = GetComponent <AudioSource> ();
playerShooting = GetComponentInChildren <PlayerShooting> ();
currentHealth = startingHealth;
CapsuleCollider = GetComponent <CapsuleCollider> ();
}
void Update ()
{
if(damaged)
{
damageImage.color = flashColour;
}
else
{
damageImage.color = Color.Lerp (damageImage.color, Color.clear, flashSpeed * Time.deltaTime);
}
damaged = false;
}
void OnCollisionEnter(Collider other)
{
if(other.gameObject.tag == "FirstAid")
{
CapsuleCollider.isTrigger = true;
Regain = true;
currentHealth += 25;
healthSlider.value = currentHealth;
}
if(currentHealth >= 100 && !AtFullHealth)
{
IsAtFullHealth ();
}
}
void IsAtFullHealth ()
{
AtFullHealth = true;
Regain = false;
}
public void TakeDamage (int amount)
{
damaged = true;
currentHealth -= amount;
healthSlider.value = currentHealth;
playerAudio.Play ();
if(currentHealth <= 0 && !isDead)
{
Death ();
}
}
void Death ()
{
isDead = true;
playerShooting.DisableEffects ();
playerAudio.clip = deathClip;
playerAudio.Play ();
playerShooting.enabled = false;
Application.LoadLevel ("LoseScene");
}
}
second code begins here
using UnityEngine;
using System.Collections;
public class HealthUp : MonoBehaviour
{
GameObject player;
bool PickedUp;
PlayerHealth playerHealth;
BoxCollider BoxCollider;
void Awake ()
{
player = GameObject.FindGameObjectWithTag ("Player");
BoxCollider = GetComponent <BoxCollider> ();
playerHealth = player.GetComponent <PlayerHealth> ();
}
void OnTriggerEnter (Collider other)
{
if(other.gameObject == player)
if (playerHealth.currentHealth <= 100)
{
BoxCollider.isTrigger = true;
Destroy (gameObject);
}
}
}
Answer by FortisVenaliter · May 13, 2015 at 09:09 PM
Honestly, I would remove the functionality from the second class, and in the collision code for the first one, when it collides with a powerup, add:
Destroy(other.gameObject);
If you're getting errors though, pay close attention, because they will always* lead you to the problem. (or at least google or post them if you don't know what they mean, so we can explain).
Another thing is to always understand what every single line of code does. Don't import from another project unless you know what that code is doing and how. I've been programming for a decade and I still follow that rule because any time I don't, I end up with headaches.
*well, at least 99.9% of the time.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Need help calling a variable from another C# script 1 Answer
Distribute terrain in zones 3 Answers
Health Script/Targeting system c# 2 Answers