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 Bonfi_96 · Oct 01, 2013 at 08:46 PM · c#coroutinebeginneruse

C#, is this how you use a coroutine properly?

I'm trying to understand coroutines, so I tried to apply them to a little game.

The script works perfectly but I don't know if this is the proper way to use a coroutine. Is there any alternative to what I did?

 using UnityEngine;
 using System.Collections;
 
 public class scriptEnemy : MonoBehaviour {
     //Inspector variables
     public Color[] shapeColor; 
     public int numberOfClicks = 2 ;
     public float waitTime = 1f;
     
     //Private variables
     
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         if(numberOfClicks <= 0)
         {
             Vector3 position = new Vector3(Random.Range(-9f, 9f), Random.Range(-5f, 5f), 0);
             RespawnWaitTime();
             RandomColor();
             StartCoroutine(RespawnWaitTime());
             transform.position = position;
             numberOfClicks = 2;
         }
     }
     
     //Used to hide the object for a certain amount of time
     IEnumerator RespawnWaitTime()
     {
         renderer.enabled = false;
         yield return new WaitForSeconds(waitTime);        
         renderer.enabled = true;
     }
     
     //used to change the material color of an objects
     void RandomColor()
     {
         if(shapeColor.Length > 0){
             float newColor = Random.Range (0f,(float)shapeColor.Length);
             renderer.material.color = shapeColor[(int)newColor];
         }
     }
 }
 
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
0
Best Answer

Answer by YoungDeveloper · Oct 01, 2013 at 08:48 PM

Considering if what interests you are basically these two blocks.

 StartCoroutine(RespawnWaitTime());

and

 IEnumerator RespawnWaitTime()
 {
 renderer.enabled = false;
 yield return new WaitForSeconds(waitTime);
 renderer.enabled = true;
 }

Then yes, everything is super puper, if we are talking about code itself. But there's one thing, note that you are calling it from update function, so function RespawnWaitTime() will be called each frame. If you want to call only one instance of that function, check out this topic i gave answer for: http://answers.unity3d.com/questions/541018/wow-my-enemies-are-pretty-strong.html It's the same problem but the only difference it's in ongui and js.

Comment
Add comment · 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

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

15 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Nested Coroutines Waiting for Each Other 1 Answer

[RESOLVED! :D][C#] Friction/Deceleration - Compiler won't let me damp individual Rigidbody axes to 0. Force in opposite direction maybe? 2 Answers

How do I update the position of different prefabs to the current position? 0 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