Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Dragunas · Feb 03, 2021 at 01:21 PM · ienumeratorhowcountdownwhy

Enable and disable component repeatedly under condition

i wanted to make a flash effect if my hunger reaches the number 20. I wanted to enable my outline and than disable it after 2 sec. and than enable it again and do this whole thing again until my hunger is more than 20 but my script starts spaming my Debug.Log and is turning my outline on and off and is not waiting the 2 sec. why?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 using System;
 
 public class AEffects : MonoBehaviour
 {
     private PlayerMovement playerMovement;
     private Outline outline;
     // Start is called before the first frame update
     void Start()
     {
         outline = GetComponent<Outline>();
 
         playerMovement = FindObjectOfType<PlayerMovement>();
     }
 
     // Update is called once per frame
     void Update()
     {
         
 
         if (playerMovement.hunger <= 20)
         {
             outline.enabled = true;
             StartCoroutine(Blincking());
         }
         if(playerMovement.hunger >= 20)
         {
             outline.enabled = false;
         }
     }
 
     IEnumerator Blincking()
     {
         outline.enabled = true;
         yield return new WaitForSeconds(2);
         Debug.Log("Time is up");
         outline.enabled = false;
         yield return new WaitForSeconds(2);
 
     }
 }

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 Hellium · Feb 03, 2021 at 01:23 PM

Since Update runs every frame, you are starting a new coroutine every frame as long as the hunger is less than 20. You need to check the coroutine is not already running. You also need to make your coroutine loop (FOLLOWING CODE NOT TESTED)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 using System;
 
 public class AEffects : MonoBehaviour
 {
     private PlayerMovement playerMovement;
     private Outline outline;
     private bool outlineBlinking;
     
     // Start is called before the first frame update
     void Start()
     {
         outline = GetComponent<Outline>();
 
         playerMovement = FindObjectOfType<PlayerMovement>();
     }
 
     // Update is called once per frame
     void Update()
     {
         if (playerMovement.hunger < 20 && outlineBlinking == false)
         {
             outlineBlinking = true;
             StartCoroutine(Blinking());
         }
         else if(playerMovement.hunger >= 20 && outlineBlinking == true)
         {
             StopAllCoroutines();
             outlineBlinking = false;
             outline.enabled = false;
         }
     }
 
     IEnumerator Blinking()
     {
         WaitForSeconds wait = new WaitForSeconds(2);
         while(true)
         {
             outline.enabled = true;
             yield return wait;
             outline.enabled = false;
             yield return wait;
         }
     }
 }

However, coroutines are not needed in your case since you already use Update. You just need a little bit of logic to handle the waits (CODE NOT TESTED)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 using System;
 
 public class AEffects : MonoBehaviour
 {
     private PlayerMovement playerMovement;
     private Outline outline;
     private float timer;
     
     // Start is called before the first frame update
     void Start()
     {
         outline = GetComponent<Outline>();
 
         playerMovement = FindObjectOfType<PlayerMovement>();
     }
 
     // Update is called once per frame
     void Update()
     {
         if (playerMovement.hunger < 20)
         {
             timer += Time.deltaTime;
             outline.enabled = Mathf.Repeat(timer, 4) < 2f;
         }
         else
         {
             outline.enabled = false;
             timer = 0;
         }
     }
 }
Comment
Add comment · Show 2 · 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 Dragunas · Feb 03, 2021 at 02:08 PM 0
Share

i tried your code but it didnt worked what could i do?

avatar image Dragunas · Feb 03, 2021 at 02:10 PM 0
Share

my fault, it works. Thanks!

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

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

My code is not working - HELP - IEnumerator Coroutines 0 Answers

Count down timer C# 7 Answers

Loading Winning UI when the timer reaches 0? 0 Answers

Canvas problem 0 Answers

Looking at a Object for 5 Seconds then Teleport 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