I am currently using the Brackeys Inventory, which I have modified slightly and it works fine. However, there is an item I created called Healing Gem, which adds 1 per second to the “health” int in my script. This is not getting any errors; rather, it is not functioning as I please. The issues are:
- Firstly, the regen is not +1 per second, it’s more like +99 per second, restoring the full health instantly.
- Secondly, it always takes the “health” value above 100, to exactly 101, even though I set it up in an if statement to reset the health value to 100 if it goes above.
- Finally, I am unsure of any other errors, however I intend this effect to be on as long as the item is equipped.
These are the two scripts that are being used:
1 - Vitals script(C#); handles basic attributes and states. Also where Coroutines are.
using UnityEngine;
using System.Collections;
public class Vitals : MonoBehaviour {
public static int health = 100;
public static int mana = 100;
public bool poisoned = false;
public bool dead = false;
public bool slow = false;
public static bool hasHealGem = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Debug.Log (health);
if(health == 0 || health < 0) {
dead = true;
}
if(health > 100) {
health = 100;
}
if(dead == true) {
Destroy(this.gameObject);
}
}
void OnTriggerEnter(Collider other) {
if (other.gameObject.CompareTag ("Damage")) {
health -= 5;
}
if(other.gameObject.CompareTag("Poison") & !poisoned) {
StartCoroutine(Poisoning());
}
if(other.gameObject.CompareTag("Slow") & !slow) {
Movement.speed -= 4;
}
if(other.gameObject.CompareTag("Kill")) {
Destroy(this.gameObject);
}
}
IEnumerator Poisoning() {
poisoned = true;
health -= 2;
for(int i = 0; i<11; i++)
{
yield return new WaitForSeconds(1);
health -= 2;
}
poisoned = false;
}
public static IEnumerator HealingGem() {
hasHealGem = true;
health += 1;
for(int i = 0; i>0; i++)
{
yield return new WaitForSeconds(1);
}
}
}
2 - ItemEffect script(JavaScript); Handles effects when items are equipped. Comes with the Inventory Package.
#pragma strict
//This script allows you to create equipment effects that will be called either OnEquip or WhileEquipped. This is usefull for magic effects and stat handling.
@script AddComponentMenu ("Inventory/Items/Equipment Effect")
@script RequireComponent(Item)
private var effectActive = false;
function Update ()
{
if (effectActive == true)
{
if(this.gameObject.CompareTag("HealGem")) {
Vitals.hasHealGem = true;
StartCoroutine(Vitals.HealingGem());
}
//-----> THIS IS WHERE YOU INSERT CODE YOU WANT TO EXECUTE AS LONG AS THE ITEM IS EQUIPPED. <-----
}
}
function EquipmentEffectToggle (effectIs : boolean)
{
if (effectIs == true)
{
effectActive = true;
Debug.LogWarning("Remember to insert code for the EquipmentEffect script you have attached to " + transform.name + ".");
//-----> THIS IS WHERE YOU INSERT CODE YOU WANT TO EXECUTE JUST WHEN THE ITEM IS EQUIPPED. <-----
}
else
{
effectActive = false;
//-----> THIS IS WHERE YOU INSERT CODE YOU WANT TO EXECUTE JUST WHEN THE ITEM IS UNEQUIPPED. <-----
}
}
I have already placed the C# Vitals script in the Standard Assets folder and everything functions as I please… Except the accelerated regeneration rate.
Please help me if you can. Thanks in advance!