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 /
  • Help Room /
avatar image
1
Question by leoxauditore · Jun 10, 2021 at 07:30 PM · scripting problemcoroutinescripting beginner

Problem with array and coroutine...Sometimes Works!!!

hi guys, i need help with something, i'm a bit of a newbie to C#, and i'm developing a horror video game.

One of the things I want this code to do is to make is when the player goes through a trigger ( static bool in other Sript) certain lights ( in an array) turn off for a few seconds and then reappear, it sounds simple, doesn't it? The point is that I do not know why this code sometimes works and sometimes it does not, in fact lately the number of times it is not working is more. Then I leave the code. I hope you can help me. Even if you know of a better or more optimized, or less bugged way to do this, I would also be very grateful if you would let me know. Greetings friends, thank you very much in advance.

 public class ParpadearLuces : MonoBehaviour
 {
     public GameObject[] _LucesSubterraneo;
     public GameObject craneo;
    
      // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         if (CalaveraTrigger.apagarLuces==true)
         {
             StartCoroutine(esperar10Segundos());
            
     }
 
   void ApagarLuces()
     {
         for (int i = 0; i < _LucesSubterraneo.Length; i++)
             _LucesSubterraneo[i].SetActive(false);
 
     }
 
      IEnumerator desaparecerCraneo()
     {
         yield return new WaitForSeconds(1);
         craneo.SetActive(false);
             
             for (int i = 0; i < _LucesSubterraneo.Length; i++)
                 _LucesSubterraneo[i].SetActive(true);
             CalaveraTrigger.apagarLuces = false;
             Destroy(this);
     }
 
     IEnumerator esperar10Segundos()
     {
             
             yield return new WaitForSeconds(10);
             ApagarLuces();
             StartCoroutine(desaparecerCraneo());
     }
     }
 }


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 xxmariofer · Jun 11, 2021 at 08:08 AM

You have an issue with the }, and also you need to set CalaveraTrigger.apagarLuces back to false, or it will call apagar luces infinite times and will create unexpected behaviour

  public class ParpadearLuces : MonoBehaviour
  {
      public GameObject[] _LucesSubterraneo;
      public GameObject craneo;
     
       // Start is called before the first frame update
      void Start()
      {
          
      }
  
      // Update is called once per frame
      void Update()
      {
          if (CalaveraTrigger.apagarLuces==true)
          {
              StartCoroutine(esperar10Segundos());
           }
      }
  
    void ApagarLuces()
      {
          for (int i = 0; i < _LucesSubterraneo.Length; i++)
              _LucesSubterraneo[i].SetActive(false);
  
      }
  
       IEnumerator desaparecerCraneo()
      {
          yield return new WaitForSeconds(1);
          craneo.SetActive(false);
              
              for (int i = 0; i < _LucesSubterraneo.Length; i++)
                  _LucesSubterraneo[i].SetActive(true);
              CalaveraTrigger.apagarLuces = false;
              Destroy(this);
      }
  
      IEnumerator esperar10Segundos()
      {
              CalaveraTrigger.apagarLuces=true;
              yield return new WaitForSeconds(10);
              ApagarLuces();
              StartCoroutine(desaparecerCraneo());
      }
  }
Comment
Add comment · Show 5 · 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 leoxauditore · Jun 11, 2021 at 06:38 PM 1
Share

Thank you so much. Your answer could be the solution. As you mentioned, I have reviewed the script and I have realized that I had already set the bool to FALSE in the "desaparecerCraneo()" coroutine However, to prevent the bool (CalaveraTrigger.apagarLuces ) from staying false I have added the command to destroy the gameObject at the end of the coroutine (is that the correct way to do it? Are there better ways?). On the other hand, I insist on understanding why sometimes it works and sometimes not, since in many of the scripts that I use coroutines I have this problem. I will be attentive to a possible answer.

avatar image leoxauditore · Jun 11, 2021 at 07:09 PM 1
Share

In these $$anonymous$$utes I have realized that it is better that in voidOnTriggerEnter () go a boolean in true inside it. And in the update () put an if (boolean == true) {execute the coroutine) ... this way it works every time. Although I still have doubts about whether destroying the object is a good way to do what I was talking about in the previous comment.

avatar image xxmariofer leoxauditore · Jun 11, 2021 at 08:53 PM 0
Share

Hello,

No deleting the gameobject to avoid "apagarluces" to be set to false is not a good solution. Update gets called every frame, and since you are waiting 1 second to reset the variable to false you are starting (if your game runs at 60fps) 60 times.

To avoid this, as i suggested set

               CalaveraTrigger.apagarLuces=false;

in the previous code i posted there was an error, change

       IEnumerator esperar10Segundos()
       {
               CalaveraTrigger.apagarLuces=true;
               yield return new WaitForSeconds(10);
               ApagarLuces();
               StartCoroutine(desaparecerCraneo());
       }
 
 to
 
 
       IEnumerator esperar10Segundos()
       {
               CalaveraTrigger.apagarLuces=false;
               yield return new WaitForSeconds(10);
               ApagarLuces();
               StartCoroutine(desaparecerCraneo());
       }


avatar image leoxauditore · Jun 12, 2021 at 01:02 AM 0
Share

You solved my life ... thank you, all the bugs were solved, it is incredible how I did not see these things. I guess experience will help me. Thank you. :)

avatar image xxmariofer leoxauditore · Jun 13, 2021 at 08:18 AM 0
Share

You are welcome, if it worked please accept the answer as correct for future readers

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

271 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Merge Points collection system via a separate script 0 Answers

Hi, we are currently doing our thesis, it's a quiz type of game. My question is, how do I collect the scores from four different scripts and output them on the menu page? 0 Answers

OnTriggerEnter2D problem 1 Answer

Can somebody help me fix this script? 2 Answers

Script problem: Trying to make the code of this script run at a set interval (GUI handle object) 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