Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Am I going crazy?

Discussion in 'Scripting' started by Venatal, Jan 27, 2016.

  1. Venatal

    Venatal

    Joined:
    Nov 12, 2015
    Posts:
    134
    For this code shouldn't the time decrease, in game it seems as if it is increasing
    Code (CSharp):
    1.  
    2. yield return new WaitForSeconds(Maxtime);
    3.  
    4.             }
    5.     }
    6.  
    7.     void SpawnTime(float time)
    8.     {
    9.         if (Maxtime - time > Mintime)        
    10.             Maxtime -= time;
    11.        
    12.         else
    13.             Maxtime = Mintime;
    14.     }
    15.  
     
  2. Mich_9

    Mich_9

    Joined:
    Oct 22, 2014
    Posts:
    118
    Can you explain what your code is supposed to do? It seems confusing.
     
  3. Venatal

    Venatal

    Joined:
    Nov 12, 2015
    Posts:
    134
    Sure, so when SpawnTime is called it should first check if the time is greater than the minimum time if true then it should decrease the time if not the maximum time should = the minimum time.
    the yield return is part of a coroutine.
     
  4. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Seems like gibberish.
     
  5. Venatal

    Venatal

    Joined:
    Nov 12, 2015
    Posts:
    134
    I don't follow?
     
  6. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    What you posted is entirely invalid code, and I'm not sure what you actually what it to do, so it is gibberish?
     
  7. Mich_9

    Mich_9

    Joined:
    Oct 22, 2014
    Posts:
    118
    Well, it seems that your code is doing exactly what you want, so what is the problem?
     
  8. arkon

    arkon

    Joined:
    Jun 27, 2011
    Posts:
    1,122
    it is gibberish but here's what you probably want to do. I think your code is falling foul of order of precedence, you can't write your if statements like that. try like this:
    Code (CSharp):
    1. void SpawnTime(float time)
    2.     {
    3.         if (time > Mintime)      
    4.             Maxtime -= time;
    5.          else
    6.             Maxtime = Mintime;
    7.     }
     
  9. Venatal

    Venatal

    Joined:
    Nov 12, 2015
    Posts:
    134
    Basically it controls the spawn time of some objects in game however it seems that every time its called the spawn time increases when it should decrease.
    I think I am going crazy :S
     
  10. arkon

    arkon

    Joined:
    Jun 27, 2011
    Posts:
    1,122
    just a hint, use monodevelop and stick a break point on the beginning of the function and single step through and watch your variables change, this will help you in future to better learn how to code and show you in this case what is going wrong.
     
  11. Venatal

    Venatal

    Joined:
    Nov 12, 2015
    Posts:
    134
    Code (CSharp):
    1. void SpawnTime(float time)
    2.     {
    3.         if (time > Mintime)      
    4.             Maxtime -= time;
    5.          else
    6.             Maxtime = Mintime;
    7.     }
    [/QUOTE]

    Theses are the three float values:
    float time = 0.05f;
    float Maxtime = 1.15f;
    float Mintime = 0.9f;
     
  12. Venatal

    Venatal

    Joined:
    Nov 12, 2015
    Posts:
    134
    Thanks for the advice!:)
     
  13. Mich_9

    Mich_9

    Joined:
    Oct 22, 2014
    Posts:
    118
    Let me see, ¿do you want to set a timer but it can't be greater than Maxtime and less than Mintime?
     
  14. Venatal

    Venatal

    Joined:
    Nov 12, 2015
    Posts:
    134
    The idea is for it to start at the max time then decrease till it reaches the min time then doesn't change.
     
  15. Venatal

    Venatal

    Joined:
    Nov 12, 2015
    Posts:
    134
    Thanks for all the responses but I figured it out!
     
  16. Venatal

    Venatal

    Joined:
    Nov 12, 2015
    Posts:
    134
    Sorry, but what is Invalid code? I'm still pretty new to coding.
     
  17. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,663
    When someone says "invalid code" they mean it won't work right. The sample you posted in the first post you cannot simply copy and paste into the IDE and have it compile properly. So yes, invalid code = not working code.
     
  18. Venatal

    Venatal

    Joined:
    Nov 12, 2015
    Posts:
    134
    Ohh, I get it. Thanks!