Search Unity

My error about COROUTINE and DATETIME.. Who know this best?? Please help me

Discussion in 'Scripting' started by meyto927, Aug 16, 2019.

  1. meyto927

    meyto927

    Joined:
    Aug 16, 2019
    Posts:
    30
    IndexOutOfRangeException: Index was outside the bounds of the array. HealthTime+d__12.MoveNext () (at Assets/Scripts/HealthTime.cs:58) UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.Networking;
    6. using UnityEngine.UI;
    7.  
    8. public class HealthTime : MonoBehaviour
    9. {
    10.  
    11.     private string url = "http://worldtimeapi.org/api/timezone/Europe/Istanbul";
    12.  
    13.     public string sDate = "";
    14.     public string sSaat = "";
    15.     public string[] splitSaat = null;
    16.     public int health;
    17.     public Text healthT;
    18.     private DateTime _dateNow;
    19.     private DateTime _firstTime;
    20.     private DateTime _endTime;
    21.     private bool a = false;
    22.  
    23.  
    24.     // Start is called before the first frame update
    25.     void Start()
    26.     {
    27.  
    28.         InvokeRepeating("Kalan", 1f, 1f);
    29.         health = 10;
    30.         a = false;
    31.         healthT.text = PlayerPrefs.GetInt("Health").ToString();
    32.         StartCoroutine(GetText());
    33.  
    34.     }
    35.  
    36.     public IEnumerator GetText()
    37.     {
    38.  
    39.         UnityWebRequest www = UnityWebRequest.Get("www.google.com");
    40.         yield return www.SendWebRequest();
    41.         if (string.IsNullOrEmpty(www.downloadHandler.text))
    42.             Debug.Log("no internet");
    43.         else
    44.         {
    45.             Debug.Log("yes internet");
    46.             StartCoroutine(NetSaat());
    47.         }
    48.  
    49.     }
    50.  
    51.     public IEnumerator NetSaat()
    52.     {
    53.        
    54.         UnityWebRequest www = new UnityWebRequest();
    55.         www = UnityWebRequest.Get(url);
    56.         yield return www.SendWebRequest();
    57.         splitSaat = www.downloadHandler.text.Split(new string[] { "utc_datetime\":\"" }, StringSplitOptions.None);
    58.         sSaat = splitSaat[1].Substring(0, 19);
    59.         yield return new WaitForSeconds(1);
    60.         _dateNow = Convert.ToDateTime(sSaat);
    61.         Debug.Log(_dateNow);
    62.         if (a==false)
    63.         FirstTime();
    64.  
    65.     }
    66.     public void basla()
    67.     {
    68.        StartCoroutine( NetSaat());
    69.     }
    70.  
    71.     public void Reward()
    72.     {
    73.      
    74.         health = PlayerPrefs.GetInt("Health");
    75.         health++;
    76.         PlayerPrefs.SetInt("Health", health);
    77.         healthT.text = health.ToString();
    78.     }
    79.     public void FirstTime()
    80.     {
    81.        
    82.         _firstTime = _dateNow;
    83.        
    84.  
    85.         EndTime();
    86.     }
    87.     public void EndTime()
    88.     {
    89.         _endTime = _firstTime.AddSeconds(5);
    90.         a = true;
    91.        
    92.     }
    93.     public void Kalan()
    94.     {
    95.         if (a == true)
    96.         {
    97.             basla();
    98.            
    99.  
    100.             if (_dateNow >= _endTime)
    101.             {
    102.                 Reward();
    103.                 a = false;
    104.                 Debug.Log(health);
    105.                
    106.                
    107.             }
    108.         }
    109.        
    110.        
    111.            
    112.  
    113.        
    114.        
    115.            
    116.        
    117.     }
    118.     // Update is called once per frame
    119.     void Update()
    120.     {
    121.        
    122.        
    123.  
    124.     }
    125.  
    126.  
    127. }
    128.  
     
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    It looks like you have an index was outside the bounds of an array, use Debug.Log to find out where and why. I suspect it's in your split logic, break that up into pieces find out where.
     
  3. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Add Debug.Log(www.downloadHandler.text) after yield return and post what it printing to console. Also after yield you should check the request for errors before using it's data. It may be 404 or 500 for instance or whatever. And why are you using so strange delimiter to split your text? What text do you expect to get here?
     
  4. meyto927

    meyto927

    Joined:
    Aug 16, 2019
    Posts:
    30
    just i am trying to get time from internet.. and i find error.. this url giving me "Forbidden" after too many wants.. How can i want less than i did? i want to check all seconds
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If you're going to spam web requests over and over endlessly to someone else's website, getting banned should be what you expect to happen. If you actually shipped a game which did this, you're effectively shipping a DDOS tool against that web site. They might even take legal action against you.

    There is no reason to be requesting the time more than once every few hours. You request the time, and use that to generate an offset from the time the device reports.
     
    Suddoha likes this.
  6. meyto927

    meyto927

    Joined:
    Aug 16, 2019
    Posts:
    30
    if i will request time for every 2 seconds.. it can be okey??
     
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    No
     
  8. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    From their FAQ page:

    It tells you right there not to do what you're doing. You can send them an email to ask what a "short timeframe" means to them, but I'd suggest that if you're making requests more than once per hour on a regular basis that you're making requests far too often. If you don't like it, set up your own time server that you can hammer as much as you want. Don't abuse someone else's free service like this.
     
  9. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Why would you request the time every two seconds? You generally check the time at the beginning of the game, if you need to.
     
    Joe-Censored likes this.
  10. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Yeah, no. Get the time from the API once, when your game starts. That's it.

    If you want to request time every second, get your own server, that you pay for.
     
    Joe-Censored likes this.
  11. meyto927

    meyto927

    Joined:
    Aug 16, 2019
    Posts:
    30
    i want to make timer for add health for every 3 minute..
     
  12. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Well you don't need to check Instanbul time to do that! Just check the time once at the beginning of the game.
     
  13. meyto927

    meyto927

    Joined:
    Aug 16, 2019
    Posts:
    30
    but if 3 minute is done .. i want to give +1 health.. if i will not check all time they can change phone time and this 3 minute done quickly?
     
  14. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Don't worry about that. Just get it working for the 99.999% of all users who would NOT change their time. Then (and only then) if it becomes a big problem, THEN check a server time, and compare your 3 minute elapsed time to theirs.
     
  15. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Get the time from the website 1 time, and generate an offset from your device's DateTime.Now. Use DateTime.Now + offset for the rest of the time your game is running. You can every hour or 2 request the time again from the server to update the offset if you really need time to stay perfectly in sync, but that probably isn't even needed.

    Or again set up your own time server you can flood with requests as much as you please.

    Imagine you're the organization which is putting up this free time server, and someone else goes and creates a game which sends a request once per second to it. Then that game gets popular with 50,000 people playing it, so now you're receiving 50,000 time requests per second taking down your free time server. That is a really heartless thing for that game developer to do to you. Why would you want to be that guy?
     
  16. meyto927

    meyto927

    Joined:
    Aug 16, 2019
    Posts:
    30
    soo i will try it every gameopen.. soo how can i health++ every 3 min on this code? use this internet date. if it will open game how much health came??
     
  17. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I've explained how to do it more than once already.
     
  18. meyto927

    meyto927

    Joined:
    Aug 16, 2019
    Posts:
    30
    but how can use it in this code?? whr is the place for equal datetimes
     
  19. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    You can prevent cheating while only checking time online once each time the game starts.
    1) At start, retrieve time and store this value (possibly in PlayerPrefs).
    2) Use the local clock to provide your timed powerups as long as the game is open. Each time they get a powerup, store the time the powerup was gotten at.
    3) Next time the game starts, retrieve the time online, and compare it to the value stored in PlayerPrefs from step 2. If the online time is less than the stored time, the player has cheated, and you can take whatever punitive actions you need to (reducing health by that much, etc).

    It's also easy and cheap to set up your own server to do this and get the time as often as you like.
     
    Joe-Censored likes this.
  20. meyto927

    meyto927

    Joined:
    Aug 16, 2019
    Posts:
    30
    i need playerprefs for quitgame time?
     
  21. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
  22. meyto927

    meyto927

    Joined:
    Aug 16, 2019
    Posts:
    30
  23. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    You're already waiting for 1 second in your coroutine. Either add more seconds, or add a counter up to 3 minutes.

    yield return new WaitForSeconds(1);
     
  24. meyto927

    meyto927

    Joined:
    Aug 16, 2019
    Posts:
    30
  25. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    After 2 days of what? I don't follow. Seems like your first task is to create a 3 minute working timer, and go from there.
     
    Joe-Censored likes this.
  26. meyto927

    meyto927

    Joined:
    Aug 16, 2019
    Posts:
    30
    every 3 minute add 1 health.. but its working on offline..
     
  27. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Nothing will change in your app if it isn't running. Do you want to find out the last time they quit the app and count the number of minutes and divide by 3 to get how many health points they've earned? So the longer I don't play the app, the more health I earn? Again, I think you should get the common case working, then customize as necessary.
     
    Joe-Censored likes this.
  28. meyto927

    meyto927

    Joined:
    Aug 16, 2019
    Posts:
    30
    they have 3/10 health.. and they can earn 1 health every 3 minute. they can be offline. if 1 player close game with 3 /10 health. if he come back after 6 min. his health will be 5 /10.. but he will come after 2 or 3 days or hours.. his health have tp be fulll. 10/ 10.. what i need check?
     
  29. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Apparently he wants to implement something that's like those game session-independent timers in long-term games. For instance, build timers like those in Clash of Clans and all of that stuff.

    Timestamps are helpful in this case.

    However, reading the thread, I'm afraid that's a little too advanced at the moment.
     
    Joe-Censored likes this.
  30. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    If you sure you want the user to gain health by not playing the game, that's up to you. But once again, get the 3 minute timer working first. Once that is done, you would want to use PlayerPrefs to save the last quit time. But get the timer working first.
     
    Suddoha likes this.
  31. meyto927

    meyto927

    Joined:
    Aug 16, 2019
    Posts:
    30
    i will try something thanx for all help..
     
  32. meyto927

    meyto927

    Joined:
    Aug 16, 2019
    Posts:
    30
    but we cant save quitgame time,am i riight?? because he can close game with task manager.. how can we get his quit time?
     
  33. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Have you got the 3 minute timer working first? It's hard to help you if you don't follow the suggestions that you have asked for. HOWEVER, correct it's difficult to code for OnApplicationQuit as it's unreliable as components are unloading. One solution would be to write the current time to PlayerPrefs every 5 minutes or so. Should be close enough to their quit time for the next app launch, 2 days later. But that is Version 2 of your task! (Actually make it every 3 minutes, and you can use the same timer!)
     
    Last edited: Aug 16, 2019
    Joe-Censored likes this.
  34. meyto927

    meyto927

    Joined:
    Aug 16, 2019
    Posts:
    30
    yea i can make 3 min timer.. but i want to learn how can i know when he is quit.. it is impossible , it think
     
  35. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    I just explained how.
     
    Joe-Censored likes this.
  36. meyto927

    meyto927

    Joined:
    Aug 16, 2019
    Posts:
    30
    so i cant do onapplicationquit =) thanx for help.. i will do it for every addhealrh later.. so its 3 min.
     
  37. meyto927

    meyto927

    Joined:
    Aug 16, 2019
    Posts:
    30
    but people cant love it.. it can be last 20 sec.. ifu close game and open.. it is 3 min again :/
     
  38. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Sorry I don't follow, people can't love it? Perhaps a translation issue. What is 20 seconds. I've explained how you can get the closest to their quit time. The next time they launch, check the time and compare to the last saved time, which will always be accurate to less than 3 minutes.
     
  39. meyto927

    meyto927

    Joined:
    Aug 16, 2019
    Posts:
    30
    how can i use onapplicationquit?
     
  40. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    You don't, as mentioned. Use the suggestion I made instead.
     
  41. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Please keep in mind that you just said the following:

    "...so i cant do onapplicationquit =) thanx for help.."

    followed by:

    "how can i use onapplicationquit?"
     
  42. meyto927

    meyto927

    Joined:
    Aug 16, 2019
    Posts:
    30
    but u ssaid its sooo hard. i said it because of it
     
  43. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Not hard, just not reliable to use OnApplicationQuit. Please keep us posted on your progress, probably time to get coding!
     
  44. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    OMG, every time a 3 minute timer hits and you do your add a heart thing just write the current time to disk. Not that hard. You're going around in circles with a trivial problem. Then you don't even have to deal with OnApplicationQuit or whatever you're worried about. Sorry about my tone, but you're apparently ignoring anything anyone suggests and getting hung up on minor problems.
     
    Last edited: Aug 16, 2019
  45. STARLUZ

    STARLUZ

    Joined:
    Aug 17, 2019
    Posts:
    1
    miahu? :)
     
  46. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    What does miahu mean? Were you able to resolve your issue following the directions offered here? Did you get the 3 minute timer working?