Unity Freezing on Play

I can’t ,for the life of me, figure out what’s causing this, I realized two of my scripts basically had the same dependencies, a lot of the same or similar functions being called, so I combined them into one script. For some reason, that caused Unity to freeze on play??

Here’s the script in question,

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using TMPro;
using UnityEngine.UI;
public class PointIncrementer : MonoBehaviour
{
    public int nuts;
    public double currentCoins,coinInc = 1;
    public float costIncreasePercent = 1.1f;
    float coinMultiplier = 1;
    public double[] baseCoinIncUpgradeCost;
    double[] currentUpgradeCost;
    int[] incrementUpgradeLevels;
    [SerializeField]
    double[] coinUpgradeAmt;
    public TextMeshProUGUI coinText,coinPerSecText, incrementDisplay;
    TextMeshProUGUI[] coinUpgradeText;
    public TextMeshProUGUI coinPerSecAmtText;
    public AudioSource clickSound;
    public AudioClip[] clickSounds;
    public Button[] upgradeButtons;
    int[] producerLevels;
    public double[] producerValue, baseProducerCost;
    double[] currentProducerPrices;
    public Button[] producerButtons;
    TextMeshProUGUI[] producerUpgradeText;
    float coinPerSecTimer = 1;
    AudioSource upgradeSound;
    Prestige prestige;
    void Awake()
    {
        producerLevels = new int[producerButtons.Length];
        currentProducerPrices = new double[producerButtons.Length];
        producerUpgradeText = new TextMeshProUGUI[producerButtons.Length];
        upgradeSound = GameObject.Find("UpgradeSound").GetComponent<AudioSource>();
        SaveData.data.onLoadGame += OnGameLoad;
        incrementUpgradeLevels = new int[upgradeButtons.Length];
        currentUpgradeCost = new double[baseCoinIncUpgradeCost.Length];
        coinUpgradeText = new TextMeshProUGUI[upgradeButtons.Length];
        for (int i = 0; i < upgradeButtons.Length; i++)
        {
            coinUpgradeText[i] = upgradeButtons[i].GetComponentInChildren<TextMeshProUGUI>();
        }
        SaveData.data.Load();
       
        prestige = FindObjectOfType<Prestige>();
        prestige.reset += Reset;
    }
    private void Start()
    {
        producerLevels = new int[producerButtons.Length];
        currentProducerPrices = new double[producerButtons.Length];
        producerUpgradeText = new TextMeshProUGUI[producerButtons.Length];
        for (int i = 0; i < producerButtons.Length; i++)
        {
            producerUpgradeText[i] = producerButtons[i].GetComponentInChildren<TextMeshProUGUI>();
            UpdateProducerCosts(i);
            TextModifier.TextMod.UpdateText(producerUpgradeText[i], baseProducerCost[i], "Upgrade Production " + (i + 1) + "\n");
        }
        if (SaveData.data.upgradeLevels == null)
        {
            SaveData.data.upgradeLevels = new int[baseCoinIncUpgradeCost.Length];
        }
        if (incrementUpgradeLevels.Length != 0)
        {
            for (int i = 0; i < baseCoinIncUpgradeCost.Length; i++)
            {
                UpdateUpgradeCosts(i);
            }
        }
        TextModifier.TextMod.UpdateText(coinText, currentCoins, "");
        TextModifier.TextMod.UpdateText(incrementDisplay, GetIncrementValue(), "+");
    }
    public void IncrementCoins()
    {
        currentCoins += GetIncrementValue();
        TextModifier.TextMod.UpdateText(coinText, currentCoins, "");
        GameObject textToSpawn = PoolGM.Pool.RequestText();
        textToSpawn.transform.position = Input.mousePosition + new Vector3(UnityEngine.Random.Range(-50,50), UnityEngine.Random.Range(-50, 50),0);
        TextModifier.TextMod.UpdateText(textToSpawn.GetComponent<TextMeshProUGUI>(), GetIncrementValue(), "+");
        clickSound.clip = clickSounds[UnityEngine.Random.Range(0, clickSounds.Length)];
        clickSound.Play();
        SaveData.data.currentCoins = currentCoins;
    }
    double GetIncrementValue()
    {
        double coinValue = 0;
        for (int i = 0; i < coinUpgradeAmt.Length; i++)
        {
            coinValue += (coinUpgradeAmt[i] * incrementUpgradeLevels[i]) * coinMultiplier;
        }
        coinValue += 1;
        return coinValue;
    }
    public void OnGameLoad()
    {
        currentCoins = SaveData.data.currentCoins;
        nuts = SaveData.data.nuts;
        incrementUpgradeLevels = SaveData.data.upgradeLevels;
        for (int i = 0; i < incrementUpgradeLevels.Length; i++)
        {
            UpdateUpgradeCosts(i);
        }
        if (SaveData.data.producerLevels.Length != 0)
        {
            producerLevels = new int[producerButtons.Length];
            for (int i = 0; i < producerLevels.Length; i++)
            {
                producerLevels[i] = SaveData.data.producerLevels[i];
            }
        }
        else
        {
            producerLevels = new int[producerButtons.Length];
            SaveData.data.producerLevels = new int[producerLevels.Length];
        }
        TextModifier.TextMod.UpdateText(coinPerSecAmtText, GetCoinsPerSecond(), "+");
        TextModifier.TextMod.UpdateText(coinText, currentCoins, "");
        TextModifier.TextMod.UpdateText(incrementDisplay, GetIncrementValue(), "+");
    }
    private void Update()
    {
        for (int i = 0; i < upgradeButtons.Length; i++)
        {
            if(currentCoins <= currentUpgradeCost[i])
            {
                upgradeButtons[i].interactable = false;
            }else
            {
                upgradeButtons[i].interactable = true;
            }
        }
        for (int i = 0; i < producerButtons.Length; i++)
        {
            if (currentCoins < baseProducerCost[i])
            {
                producerButtons[i].interactable = false;
            }
            else
            {
                producerButtons[i].interactable = true;
            }
        }
        coinPerSecTimer -= Time.deltaTime;
        if (GetCoinsPerSecond() != 0 && coinPerSecTimer < 0)
        {
            ProduceMoney();
            coinPerSecTimer = 1;
        }
    }
    void ProduceMoney()
    {
        currentCoins += GetCoinsPerSecond();
        SaveData.data.currentCoins = currentCoins;
        TextModifier.TextMod.UpdateText(coinText, currentCoins, "");
        GameObject textToSpawn = PoolGM.Pool.RequestText();
        float xPos = UnityEngine.Random.Range(50, Screen.width - 50);
        float yPos = UnityEngine.Random.Range(700, Screen.height - 500);
        textToSpawn.GetComponent<RectTransform>().anchoredPosition = new Vector3(xPos, yPos, 0);
        TextModifier.TextMod.UpdateText(textToSpawn.GetComponent<TextMeshProUGUI>(), GetCoinsPerSecond(), "+");
    }
    public double GetCoinsPerSecond()
    {
        double num = 0;
        for (int i = 0; i < producerLevels.Length; i++)
        {
            if (producerLevels[i] != 0)
            {
                num += producerValue[i] * producerLevels[i];
            }
        }
        return num;
    }
    public void BuyProducer(int index)
    {
        if (currentCoins >= baseProducerCost[index])
        {
            upgradeSound.Play();
            currentCoins -= baseProducerCost[index];
            producerLevels[index]++;
            UpdateProducerCosts(index);
            TextModifier.TextMod.UpdateText(coinPerSecAmtText, GetCoinsPerSecond(), "");
            TextModifier.TextMod.UpdateText(coinText, currentCoins, "");
            SaveData.data.producerLevels = producerLevels;
        }
    }
    public void UpgradeCoinIncrement(int upgradeIndex)
    {
        if(currentCoins >= currentUpgradeCost[upgradeIndex])
        {
            upgradeSound.Play();
            currentCoins -= currentUpgradeCost[upgradeIndex];
            incrementUpgradeLevels[upgradeIndex]++;
            UpdateUpgradeCosts(upgradeIndex);
            TextModifier.TextMod.UpdateText(coinText, currentCoins, "");
            TextModifier.TextMod.UpdateText(incrementDisplay,GetIncrementValue(),"");
            SaveData.data.upgradeLevels = incrementUpgradeLevels;
        }
    }
    void UpdateUpgradeCosts(int index)
    {
            currentUpgradeCost[index] = baseCoinIncUpgradeCost[index] * Math.Pow(costIncreasePercent,incrementUpgradeLevels[index]);
            if (currentUpgradeCost[index] == 0) currentUpgradeCost[index] = baseCoinIncUpgradeCost[index];
            TextModifier.TextMod.UpdateText(coinUpgradeText[index], currentUpgradeCost[index], "Upgrade Tap Value " + (index + 1) + "\n");
    }
    void Reset()
    {
        for (int i = 0; i < incrementUpgradeLevels.Length; i++)
        {
            incrementUpgradeLevels[i] = 0;
            UpdateUpgradeCosts(i);
        }
        for (int i = 0; i < producerLevels.Length; i++)
        {
            producerLevels[i] = 0;
            UpdateProducerCosts(i);
        }
        currentCoins = 0;
        TextModifier.TextMod.UpdateText(coinText, currentCoins, "");
        TextModifier.TextMod.UpdateText(incrementDisplay, GetIncrementValue(), "");
    }
    void UpdateProducerCosts(int index)
    {
        currentProducerPrices[index] = baseProducerCost[index] * Math.Pow(costIncreasePercent, producerLevels[index]);
        if (currentProducerPrices[index] == 0) currentProducerPrices[index] = baseProducerCost[index];
        TextModifier.TextMod.UpdateText(producerUpgradeText[index], currentProducerPrices[index], "Upgrade Production " + (index + 1) + "\n");
            UpdateProducerCosts(index);
       
    }
}

I know it’s a bulky script, I have yet to clean it up after moving everything over :stuck_out_tongue:

But, I don’t understand it, I don’t have any while loops or anything, it definitely is this script, as it doesn’t freeze if I disabled the object this is attached to.

Thanks in advance

How big are your collections? Does it still lock if you set each collection of things (whatever it is) to just one thing?

A sufficiently large number of things will cause a delay that is indistinguishable from a crash, until that delay completes and the processing is done.

Also, make sure you are not lengthening your arrays (i.e., recreating them) while you are looping over them. If you comment out the guts of these loops, does it stop locking? If so the problem is something you’re doing in the loop. If it still locks, then the lock is elsewhere.

1 Like

They’re not very long, the biggest array is 8, and it’s just an array of buttons.

Oh, I did something very stupid…

    void UpdateProducerCosts(int index)
    {
        currentProducerPrices[index] = baseProducerCost[index] * Math.Pow(costIncreasePercent, producerLevels[index]);
        if (currentProducerPrices[index] == 0) currentProducerPrices[index] = baseProducerCost[index];
        TextModifier.TextMod.UpdateText(producerUpgradeText[index], currentProducerPrices[index], "Upgrade Production " + (index + 1) + "\n");

            UpdateProducerCosts(index);
      
    }

When I was copying over everything I accidentally made this function call itself inside of itself… lol, I noticed because you suggested commenting out the guts of the loops, coincidentally the first loop I gutted was the right one.

Thanks, it works now!

1 Like