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. Dismiss Notice

Question script that makes the scene unable to load

Discussion in 'Scripting' started by unity_U3HNm92xRf11pQ, Jul 27, 2022.

  1. unity_U3HNm92xRf11pQ

    unity_U3HNm92xRf11pQ

    Joined:
    Aug 13, 2021
    Posts:
    1
    Hello, I was developing a script to be able to make an item store. Like I plan to include future items I have used lists and loops. But if I press play, the program stays in Application.EnterPlayMode, if I disable the script from the gameObject the program starts without problems.

    Apart from having the buy function, the script has the function of showing the buy button if you have money or it shows you the ad button if you don't have money.

    Hola, estaba desarrollando un script para poder hacer una tienda de items. Como tengo pensado incluir futuros items he utilizado listas y bucles. Pero si pulso play, el programa se queda en Application.EnterPlayMode, si desactivo el script desde el gameObject el programa comienza sin problemas.

    El script aparte de tener la función de comprar tiene la función de mostrar el boton de comprar si tienes dinero o te muestra el boton de anuncio si no tienes dinero.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7. public class ComprarItems : MonoBehaviour
    8. {
    9.  
    10.     public List<TextMeshProUGUI> ObjetosTexto;
    11.     public List<int> ObjetosCantidad;
    12.     public List<float> Precios;
    13.     public List<Button> BotonesNormales;
    14.     public List<Button> BotonesAnuncio;
    15.     public List<string> NombreCompra;
    16.     public List<GameObject> Marcos;
    17.     public AudioSource sonido;
    18.     private float dinero;
    19.     private float saldo;
    20.  
    21.     void Start()
    22.     {
    23.  
    24.         int i = 0;
    25.  
    26.         sonido.Stop();
    27.         dinero = PlayerPrefs.GetFloat("Saldo");
    28.  
    29.         while (ObjetosCantidad.Count > i)
    30.         {
    31.             ObjetosCantidad[i] = PlayerPrefs.GetInt(NombreCompra[i], 0);
    32.         }
    33.     }
    34.  
    35.  
    36.     void Update()
    37.     {
    38.         ComprobarBotones();
    39.         ComprobarTextos();
    40.     }
    41.  
    42.     public void Comprar(string QueCompra)
    43.     {
    44.         int i = 0;
    45.         while (i < Precios.Count)
    46.         {
    47.             if (QueCompra == NombreCompra[i])
    48.             {
    49.                 saldo = dinero - Precios[i];
    50.                 PlayerPrefs.SetFloat("Saldo", saldo);
    51.                 ObjetosCantidad[i]++;
    52.                 PlayerPrefs.SetInt(NombreCompra[i], ObjetosCantidad[i]);
    53.                 sonido.Play();
    54.             }
    55.             else if (QueCompra != NombreCompra[i])
    56.             {
    57.                
    58.             }
    59.             i++;
    60.         }
    61.     }
    62.  
    63.     private void ComprobarBotones()
    64.     {
    65.         dinero = PlayerPrefs.GetFloat("Saldo");
    66.  
    67.         int i = 0;
    68.  
    69.         while (ObjetosCantidad.Count > i)
    70.         {
    71.             if (dinero >= Precios[i])
    72.             {
    73.                 BotonesNormales[i].enabled = true;
    74.                 BotonesAnuncio[i].enabled = false;
    75.             }
    76.             else if (dinero < Precios[i])
    77.             {
    78.                 BotonesNormales[i].enabled = false;
    79.                 BotonesAnuncio[i].enabled = true;
    80.             }
    81.             i++;
    82.         }
    83.        
    84.     }
    85.  
    86.     public void Ads()
    87.     {
    88.         Debug.Log("Activar Anuncio");
    89.     }
    90.  
    91.     private void ComprobarTextos()
    92.     {
    93.  
    94.         int i = 0;
    95.  
    96.         while (ObjetosTexto.Count > i)
    97.         {
    98.             ObjetosTexto[i].text = ObjetosCantidad[i].ToString();
    99.             i++;
    100.         }
    101.  
    102.     }
    103.  
    104.     public void ActivarODesactivarMarco(int NumeroDeMarco)
    105.     {
    106.         if (Marcos[NumeroDeMarco] == true)
    107.         {
    108.             Marcos[NumeroDeMarco].SetActive(false);
    109.         }
    110.         else if (Marcos[NumeroDeMarco] == false)
    111.         {
    112.             Marcos[NumeroDeMarco].SetActive(true);
    113.         }
    114.        
    115.     }
    116. }
    117.  
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You have an infinite loop in Start(), and possibly the other loops as well.