Hi yall,
I’m having a weird problem with a specific scene crashing when this script is initialised - any tips?
Thanks in advance,
me
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class musicingm : MonoBehaviour
{
public GameObject text;
public GameObject[] musicava;
int i1;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
i1 = System.Convert.ToInt32(text); // Turns i to 100
if (i1 < 30)
{
for (int i = 0; i < 8; i++)
{
int rnd = Random.Range(0, musicava.Length);
if (!musicava[rnd].activeSelf) musicava[rnd].SetActive(true);
else i--;
}
}
}
}
Exactly as Rad Red points out… plus some observations.
Observation 1:
What is going on in line 23? Why are you taking the ToString() of a GameObject called text and then parsing it to an integer? I extremely doubt that would ever be useful.
Are you trying to parse integer data out of an InputField? If so, use the InputField and get the .text field out of there into a string, then use int.TryParse() or else you will explode the moment there’s invalid data in the InputField.
Also in that case you want to not act on the value in il when the return code from TryParse() is false, as il becomes irrelevant.
Or perhaps instead just make il a public integer you can tweak in the editor?
Observation 2:
While totally legal, manipulating a loop control variable both within the for() header and within the body is a little wonky, and especially when you are backing up the counter, which could lead to the loop not exiting, which is what Rad Red pointed out in the first place.
Well to say i dont know whats going on would be a understatment - currently trying to make a randim track play when the text string is less than 30 seconds left (note that the time is displayed in 0:00 manner) - so from what i understood i shoudnt use int32 conversion but TryParse? Thanks again mate, huge help