Hello, I’m very new to unity code, mainly work with game creator.
I just purchased a healthbar asset - which changes its health value when the up or down arrow are pressed.
I need the value to change when it collides with a tag instead (bullet / health pack).
Bellow is the original code and my failed attempt to change it
Many thanks,
John
Original
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
//Bars being used in the scene
[SerializeField]
private BarType health;
[SerializeField]
private BarType mana;
[SerializeField]
private BarType super;
[SerializeField]
private BarType healthCircle;
[SerializeField]
private BarType manaCircle;
[SerializeField]
private BarType heartFill;
//Call initialize for each bar type that you are using.
void Awake()
{
health.Initialize();
mana.Initialize ();
super.Initialize ();
healthCircle.Initialize ();
manaCircle.Initialize ();
heartFill.Initialize ();
}
//Example code to reflect changes of the bars. Keyboard input - Up/Down Arrow!
void Update ()
{
if (Input.GetKeyDown (KeyCode.UpArrow)) {
health.CurrentValue += 10;
mana.CurrentValue += 25;
super.CurrentValue += 12.5f;
healthCircle.CurrentValue += 10;
manaCircle.CurrentValue += 25;
heartFill.CurrentValue += 50;
}
if (Input.GetKeyDown (KeyCode.DownArrow)) {
health.CurrentValue -= 10;
mana.CurrentValue -= 25;
super.CurrentValue -= 12.5f;
healthCircle.CurrentValue -= 10;
manaCircle.CurrentValue -= 25;
heartFill.CurrentValue -= 50;
}
}
}
My collider/tag version (not working)
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
//Bars being used in the scene
[SerializeField]
private BarType health;
[SerializeField]
private BarType mana;
[SerializeField]
private BarType super;
[SerializeField]
private BarType healthCircle;
[SerializeField]
private BarType manaCircle;
[SerializeField]
private BarType heartFill;
//Call initialize for each bar type that you are using.
void Awake()
{
health.Initialize();
mana.Initialize ();
super.Initialize ();
healthCircle.Initialize ();
manaCircle.Initialize ();
heartFill.Initialize ();
}
void OnCollisionEnter(Collision collision)
{
if (collision.transform.CompareTag(“Health”)) {
health.CurrentValue += 10;
mana.CurrentValue += 25;
super.CurrentValue += 12.5f;
healthCircle.CurrentValue += 10;
manaCircle.CurrentValue += 25;
heartFill.CurrentValue += 50;
}
if (collision.transform.CompareTag(“Bullet”)) {
health.CurrentValue -= 10;
mana.CurrentValue -= 25;
super.CurrentValue -= 12.5f;
healthCircle.CurrentValue -= 10;
manaCircle.CurrentValue -= 25;
heartFill.CurrentValue -= 50;
}
}
}