Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by timzand · May 04, 2016 at 11:38 PM · booleanif-statementstime.deltatime

bool not properly working and time counter only adding once

Hey, I'm trying to have a counter after chopping down a tree, so I can spawn it again after a couple of seconds, but the problem is that the IF test is not reacting even though the bool is set to true...

 using UnityEngine;
 using System.Collections;
 
 public class mineTreeStaticMap : MonoBehaviour {
     
     GameObject manager;
     GameMangerINV managerScript;
     public int treeHealth = 100;
     public int currentTreeHealth;
     public float time = 0;
     public float timeToSpawn = 10;
     public bool treeDead = false;
     void Start () {
         manager = GameObject.Find("Player"); //GearMaster
         managerScript = manager.GetComponent<GameMangerINV>();
         currentTreeHealth = treeHealth;
         treeDead = false;
     }
     
     
     void Update () {
         if (currentTreeHealth <= 0) {
             gameObject.SetActive(false);
             treeDead = true;
         }
         if(treeDead == true){
             time = time + Time.deltaTime;
             Debug.Log ("TIMER:" + time);
             if(time >= timeToSpawn) {
                 gameObject.SetActive(true);
                 currentTreeHealth = treeHealth;
                 time = 0f;
             }
         }
         
     }
     
     public void getDamage(int dmg) {
         print ("fant");
         currentTreeHealth = currentTreeHealth - dmg;
         //healthslider.value = treeHealth;
     }
 }
 

Also if you have any recommendations, I would gladly take them. I'm worried about the game lagging when it counts in every frame.

Thanks.

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
1
Best Answer

Answer by TBruce · May 05, 2016 at 12:55 AM

@timzand

This should do it for you

 using UnityEngine;
 using System.Collections;
 
 public class mineTreeStaticMap : MonoBehaviour
 {
     GameObject manager;
     GameMangerINV managerScript;
     public int treeHealth = 100;
     public int currentTreeHealth;
     public float time = 0;
     public float timeToSpawn = 10;
     public bool treeDead = false;
 
     void Start ()
     {
         manager = GameObject.Find("Player"); //GearMaster
         managerScript = manager.GetComponent<GameMangerINV>();
         currentTreeHealth = treeHealth;
     }
 
 
     void Update ()
     {
         if ((currentTreeHealth <= 0) && (!treeDead))
         {
             gameObject.SetActive(false);
             treeDead = true;
             StartCoroutine(StartTimer());
         }
     }
 
     IEnumerator StartTimer()
     {
         yield return new WaitForSeconds(timeToSpawn); 
         currentTreeHealth = treeHealth;
         treeDead = false;
         gameObject.SetActive(true);
     }
 
     public void getDamage(int dmg)
     {
         print ("fant");
         currentTreeHealth = currentTreeHealth - dmg;
         //healthslider.value = treeHealth;
     }
 }
Comment
Add comment · Show 4 · 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 timzand · May 06, 2016 at 08:18 PM 0
Share

Thank you, could you or anyone elaborate some on what was wrong, so I can learn from my mistake? Especially the boolean thing...

Thanks again!

avatar image TBruce timzand · May 06, 2016 at 08:49 PM 1
Share

@timzand

The idea is this when a tree has been chopped down you want to

  1. Set the tree to dead

  2. Deactivate the tree (hide the tree)

  3. Start a countdown timer

  4. As long as the countdown timer is greater than 0 the will tree stay inactive and hidden

  5. When the countdown timer reaches 0 you want to activate the tree, mark it not dead and show the tree

In Update this if block will only be entered if the currentTreeHealth is less than or equal to 0 and the tree has not been marked as dead

 if ((currentTreeHealth <= 0) && (!treeDead))

Here (within the if block)

 gameObject.SetActive(false);
 treeDead = true;
 StartCoroutine(StartTimer());

we

  1. Deactivate the tree (which hides it)

  2. Set the boolean treeDead to true (so that the if block is not entered)

  3. Start a countdown timer (StartCoroutine(StartTimer());) which is a Coroutine

    IEnumerator StartTimer() { yield return new WaitForSeconds(timeToSpawn); currentTreeHealth = treeHealth; treeDead = false; gameObject.SetActive(true); } This statement will yield from any further code execution until timeToSpawn number of seconds is complete

    yield return new WaitForSeconds(timeToSpawn); Once timeToSpawn number of seconds is complete we

  4. Set currentTreeHealth to the default treeHealth value

  5. $$anonymous$$ark the tree as alive (treeDead = false;)

  6. Show the tree (gameObject.SetActive(true);)

avatar image timzand TBruce · May 06, 2016 at 09:36 PM 0
Share

Well explained, thanks a lot! I appreciate that you took time to explain!

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

55 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 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 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

how do i say this 1 Answer

How do I make a gun reload after 30 shots? 4 Answers

Get Time A Bool Has Been True 4 Answers

Three button combination to open next scene 2 Answers

Why doesn't this if-statement work? 1 Answer


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