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 Dy4848 · Mar 07 at 03:05 PM · coroutine errors

Ienumerator not working properly

I am working on a 3d game that involves switching between 3 lanes to avoid obstacles. I am adding a power-up and I have the spawning working, I am currently trying to add a timer that wears off after 5 seconds. My coroutine only works once and skips over the while loop inside it. I have tried starting the coroutine in Start() and Update(), but it only runs the two Debug.Log() commands once, then nothing. I have set the bool for the loop to true in multiple parts of the code but it does not seem to be working. Does anyone know what I am doing wrong? Also sorry, I couldn't get the code coloured.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerControllerSmoothMovement : MonoBehaviour
 {
     private Vector3 startPosition = new Vector3 (0, 2, -6);
     private Vector3 leftPosition = new Vector3 (-3, 2, -6);
     private Vector3 rightPosition = new Vector3 (3, 2, -6);
     Animator anim;
     private Rigidbody playerRB;
     public ParticleSystem particle;
     public Collision power;
     private float timer;
     private bool boolean;
     private IEnumerator powers;
     private float repeatRate = 0.0f;
     void Start()
     {
         powers = PowerUpTimer();
         anim = GetComponent<Animator>();
         playerRB = GetComponent<Rigidbody>();
         particle.Stop();
         timer = 0.0f;
         StartCoroutine(powers);
     }
 
     // Update is called once per frame
     void Update()
     {
         if(Input.GetKeyDown(KeyCode.A))
         {
             if(transform.position == startPosition)
             {
                 anim.SetTrigger("StartToLeft");
             }
         
             if(transform.position == rightPosition)
             {
                 anim.SetTrigger("RightToStart");
             }
         }
         if(Input.GetKeyDown(KeyCode.D))
         {
             if(transform.position == startPosition)
             {
                anim.SetTrigger("StartToRight");
             }
             if(transform.position == leftPosition)
             {
                 anim.SetTrigger("LeftToStart");
             }
         }
         
     }
     void OnTriggerEnter(Collider other)
     {
         if(other.gameObject.tag == "Obstacle")
         {
             playerRB.useGravity = true;
             anim.gameObject.GetComponent<Animator>().enabled = false; 
             playerRB.AddTorque(Vector3.left, ForceMode.Impulse);
             playerRB.AddExplosionForce(20.0f, transform.position, 20.0f, 5.0f, ForceMode.Impulse);
             particle.Play();
             StopAllCoroutines();
         }
         
         if(other.gameObject.CompareTag("PowerUp"))
         {
             boolean = true;
             timer = (timer + 5.0f);
             StartCoroutine(powers);
             
         }
         
     }
     IEnumerator PowerUpTimer()
     {   
         Debug.Log("timer is" + timer);
         while(boolean == true)
         {
             yield return new WaitForSeconds(1);
             Debug.Log("hi");
             timer = (timer - 1.0f);
             particle.Play();
             
         }
         Debug.Log("passed while loop");
         yield return new WaitForSeconds(1);
     }
 }

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 sacredgeometry · Mar 07 at 03:35 PM

This is really confused, all you need to do in your couroutine is essentially this:


 IEnumerator PowerUp()
 {   
     particleSystem.Play();
     // Add buff or enable power-up here
     yield return new WaitForSeconds(5);
     // Remove buff or disable power-up here
     particleSystem.Stop
 }
Comment
Add comment · Show 8 · 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 Dy4848 · Mar 07 at 04:26 PM 0
Share

And I just have this called when I want it activated right? Thank you for this answer.

avatar image Dy4848 Dy4848 · Mar 07 at 05:57 PM 0
Share

It only plays once, though. If the power-up gets activated again it doesn't work. Does anyone know any solutions for this?

avatar image sacredgeometry Dy4848 · Mar 07 at 11:17 PM 0
Share

StartCouroutine(PowerUp()) gets called when ever you want to power up, at the moment its on trigger enter.


What code have you put in the coroutine? It will trigger the coroutine every time you start it so I would probably wrap it in a test to see if it is in fact already started.

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

132 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

Related Questions

Coroutine Errors 0 Answers

Coroutine Scale goes past maxSize, but still keeps scaling. 2 Answers

StartCoroutine NullReferenceException: Object reference not set to an instance of an object 1 Answer

Why does this co-routine not function properly after re-entering the scene? 0 Answers

Problems with C# Coroutines 4 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