Y
Y
Yura Mailler2020-12-25 08:12:34
Unity
Yura Mailler, 2020-12-25 08:12:34

How to get rid of the infinite loop?

Hello! I do a match check in the json file and if I find it, it should run the Skin caroutine, it checks for a number and starts ItNUM where it checks. And if there are no matches, then we launch another caroutine that adds a number to json , but I start an endless loop when there is a match
, and all the conditions that exist in the check start working and everything freezes

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;


public class CharectorChans : MonoBehaviour
{
   
    private string path;

    

    public int i = 0;
    public int Rand;
    public int Num;

    public Transform pos ;

    public Sprite[] Charectors;

    private Save sv = new Save();
    
    

    
    void Start()
    {
        

#if UNITY_ANDROID && !UNITY_EDITOR
        path = Path.Combine(Application.persistentDataPath, "Save.json");
#else
        path = Path.Combine(Application.dataPath, "Save.json"); // путь файла
#endif

        if (File.Exists(path))//если файл есть
        {
            sv = JsonUtility.FromJson<Save>(File.ReadAllText(path)); // присваеваем 
            
            Debug.Log("присвоили");
        }
        else
        {

        }
        
        StartCoroutine(Skin());
    }



#if UNITY_ANDROID && !UNITY_EDITOR
        private void OnApplicationPause(bool pause){
            if(paise) File.WriteAllText(path, JsonUtility.ToJson(sv));
        }
#endif


    private void OnApplicationQuit()
    {
        File.WriteAllText(path, JsonUtility.ToJson(sv));
        Debug.Log("сохранили");
    }

    void Update()
    {
        File.WriteAllText(path, JsonUtility.ToJson(sv));

    }
    
    IEnumerator Skin(){ // узнаю какое число выполо
        yield return new WaitForSeconds(0.1f);
        Rand = UnityEngine.Random.Range(1, 85);
        if (Rand <= 30 ){
            this.GetComponent<SpriteRenderer>().sprite = Charectors[Num = 0];
            ItNUM();
        }
        else if(Rand >= 31 && Rand <= 55)
        {
            this.GetComponent<SpriteRenderer>().sprite = Charectors[Num = UnityEngine.Random.Range(1,5)];
            ItNUM();
        }
        else if(Rand >= 56 && Rand <= 70)
        {
            this.GetComponent<SpriteRenderer>().sprite = Charectors[Num = UnityEngine.Random.Range(6,7)];
            ItNUM();
        }
        else if(Rand >= 71 && Rand <= 80)
        {
            this.GetComponent<SpriteRenderer>().sprite = Charectors[Num = UnityEngine.Random.Range(8,9)];
            ItNUM();
        }
        else if(Rand >= 81 && Rand <= 85)
        {
            this.GetComponent<SpriteRenderer>().sprite = Charectors[Num = UnityEngine.Random.Range(10,11)];
            ItNUM();
        }
        
    }
    IEnumerator Wait1()
    {
        yield return new WaitForSeconds(1f);
        sv.ItsNum.Add(Num);
    }
    

    

    [Serializable]
    public class Save
    {
        public List<int> ItsNum = new List<int>();
    }


    public void ItNUM() //здесь идет проверка на json
    {
        
        Debug.Log("Робит");
        
        foreach (int item in sv.ItsNum)
        {
            Debug.Log(item);
            if(Num == item) 
            {
                
                StartCoroutine(Skin()); // если совпадает
            }
            else
            {
                StartCoroutine(Wait1()); // в ином случае  
            }
            
                
        }
        
    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question