Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DubstepDragon · Jan 05, 2014 at 01:53 PM · inventorymagic

Equipping a Healing Gem to give passive regen...

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! :D

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Invertex · Jan 05, 2014 at 02:27 PM

It looks as though you're creating a new coroutine every frame, since it's merely set to start one as long as those conditions are true. Coroutines aren't a singular entity that just gets turned on or off, when you call it in the script, it starts it in memory as its own instance, and any other time you call that code, it will start another coroutine instance, even though one is already running. And as you can imagine, that will lead to a performance loss.

I would probably do it like this:

     IEnumerator HealingGem()
     {
         while (hasHealGem)
         {
             if (health < 100)
             {
                 health ++;
             }
             yield return new WaitForSeconds(1);
         }
     }

And your other script:

 if((this.gameObject.CompareTag("HealGem")) && (!Vitals.hasHealGem)) //Now the coroutine will only be activated once when the gem is picked up, since hasHealGem will be true on the next update.
        {
          Vitals.hasHealGem = true;
          StartCoroutine(Vitals.HealingGem());
        }
Comment
Add comment · Show 11 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DubstepDragon · Jan 05, 2014 at 03:03 PM 0
Share

It only adds 1 to HP when it is equipped, and I want it to keep adding 1 per second forever. Can you help me with that please?

avatar image Invertex · Jan 05, 2014 at 03:50 PM 0
Share

Sorry about that, I simplified it and made it do a while loop. Using While will cause it to continue to do what we need until the condition is false.

avatar image DubstepDragon · Jan 05, 2014 at 08:54 PM 0
Share

It looks logically possible, however Unity3D Crashes as soon as I equip it. I tried multiple times and achieved the same results. Can this possibly be an issue of the JavaScript clashing with the C#?

avatar image Invertex · Jan 05, 2014 at 10:40 PM 0
Share

It worked fine in all C# for me, so maybe. Though I've never heard of that.

Did you make sure that there is only one Coroutine named "HealingGem"? If there are two named the same, it could cause a crash. Try removing the static modifier on the coroutine as well.

avatar image Invertex · Jan 06, 2014 at 12:01 AM 0
Share

Oops, I also had a bracket going the wrong way, don't know if you had caught that or not, but fixed it. Also, the formatting on here screwed it up a bit so it wouldn't copy/paste correctly, fixed it...

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Very simple inventory script... 0 Answers

A node in a childnode? 1 Answer

How to simplify my Equipment method? 1 Answer

BurgZerg Arcade inventory system tutorials... Incomplete!! 1 Answer

Inventory system using an array... 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges