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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

"The script needs to derive from MonoBehaviour", but it does!

Discussion in 'Scripting' started by Vini310, May 5, 2020.

  1. Vini310

    Vini310

    Joined:
    Apr 22, 2020
    Posts:
    23
    This problem I'm having is very odd, for some reason I can't add ANY script on ANYTHING, everytime I do, it displays the message "Can't add script behaviour AssemblyInfo.cs. The script needs to derive from MonoBehaviour!", even though ALL scripts derive from it! And everything was working fine until I started creating new scripts, which have some errors, but even the functional scipts are being affected!

    The first script shows the following errors:
    A) Cannot apply indexing with [] to an expression of type 'int' (MULTIPLE TIMES)
    B) Use of unassigned local variable 'boxe'
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BasicoMatriz : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.         int i;
    11.         int boxe;
    12.         int total_Round_Lutador1 = 0, total_Round_Lutador2 = 0;
    13.        
    14.         for (i = 1; i < 10; i++)
    15.         {
    16.             boxe[0, i] = Random.Range(1, 100);
    17.             boxe[1, i] = Random.Range(1, 100);
    18.         }
    19.        
    20.         for (i = 1; i < 10; i++)
    21.         {
    22.             if (boxe[0, i] > boxe[1, i])
    23.             {
    24.                 boxe[2, i] = 1;
    25.             }
    26.             else
    27.             {
    28.                 if (boxe[0, i] < boxe[1, i])
    29.                 {
    30.                     boxe[2, i] = 2;
    31.                 }
    32.                 else
    33.                 {
    34.                     boxe[2, i] = 0;
    35.                 }
    36.             }
    37.            
    38.             for (i = 1; i < 10; i++)
    39.             {
    40.                 if (boxe[2, i] == 1)
    41.                 {
    42.                     total_Round_Lutador1++;
    43.                 }
    44.                
    45.                 if (boxe [2, i] == 2)
    46.                 {
    47.                     total_Round_Lutador2++;
    48.                 }
    49.             }
    50.            
    51.             for (i = 1; i < 10; i++)
    52.             {
    53.                 print("Round " + (i) + " | Lurador 1 ( " + boxe[0, i] + ") x Lutador 2 (" + boxe[1, i] + ")");
    54.             }
    55.             print("O Lutador 1 venceu " + total_Round_Lutador1 + " Rounds");
    56.             print("O lutador 2 venceu " + total_Round_Lutador2 + " Rounds");
    57.            
    58.             if (total_Round_Lutador1 > total_Round_Lutador2)
    59.     {
    60.         print("Vencedor da luta é Lutador 1");
    61.     }
    62.     else
    63.     {
    64.         if (total_Round_Lutador1 < total_Round_Lutador2)
    65.         {
    66.             print("Vencedor da luta é o Lutador 2");
    67.         }
    68.         else
    69.         {
    70.             print("Empate técnico!");
    71.         }
    72.     }
    73.         }
    74.     }
    75.  
    76.     // Update is called once per frame
    77.     void Update()
    78.     {
    79.        
    80.     }
    81. }
    82.  
    The second script has the following error: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MegaSenaV2 : MonoBehaviour
    6. {int i;
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.         int[,] megasena = new int[1001, 7];
    11.         int[] sorteio = new int[7];
    12.        
    13.         for (i = 1; i <=6; i++)
    14.         {
    15.             sorteio[i] = Random.Range(1, 60);
    16.         }
    17.        
    18.         System.Array.Sort(sorteio);
    19.        
    20.         sorteio[1] = 1;
    21.         sorteio[2] = 1;
    22.         for (i = 1; i <= 6; i++)
    23.         {
    24.             print("Numero " + i + " (" + sorteio[i] + ")");
    25.         }
    26.        
    27.         for (i = 2; i <=6; i++)
    28.         {
    29.             if (sorteio[i - 1] == sorteio[i])
    30.             {
    31.                 sorteio[i] = Random.Range(1, 60);
    32.                 System.Array.Sort(sorteio);
    33.                 i--;
    34.             }
    35.         }
    36.        
    37.         int n, limite = 10;
    38.         for (n = 1; n <= limite; n++)
    39.         {
    40.             for (i = 1; i <= 6; i++)
    41.             {
    42.                 megasena[n, 1] = sorteio[i];
    43.             }
    44.         }
    45.        
    46.         for (n - 1; n <= limite; n++)
    47.         {
    48.             print("Megasena [" + n +"] = " +
    49.             megasena[n, 1] + ", " +
    50.             megasena[n, 2] + ", " +
    51.             megasena[n, 3] + ", " +
    52.             megasena[n, 4] + ", " +
    53.             megasena[n, 5] + ", " +
    54.             megasena[n, 6]);
    55.         }
    56.     }
    57.  
    58.     // Update is called once per frame
    59.     void Update()
    60.     {
    61.        
    62.     }
    63. }
    64.  
    What's going on? It has something to do with these scripts? Because everything is MonoBehaviour!
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Make sure that all scripts have compiled successfully. This usually happens when a compile error occurs after importing an asset and formerly correct files no longer compile. You’ll see that these scripts that you can’t add have no assembly (yet). If you can, reimport all so that they are marked as ‚need compilation‘ and then resolve any compiler errors.
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Your variable "boxe" is a simple integer as you declare on line 11:
    Code (CSharp):
    1. int boxe;
    It seems like you actually wanted it to be a two-dimensional array:
    Code (CSharp):
    1. // Two-dimensional array
    2. int[,] boxe;
    Again, same variable "boxe", you haven't assigned it a value so of course it complains.

    For the second file you have this on line 46:
    Code (CSharp):
    1.         for (n - 1; n <= limite; n++)
    The "n-1" part needs to be a full statement. Did you mean "n = 1"?

    If you have any scripts with compile errors, you basically can't do much of anything in Unity. You should fix the compile errors ASAP.
     
  4. Vini310

    Vini310

    Joined:
    Apr 22, 2020
    Posts:
    23
    Yes, it was supposed to be "n = 1", oh the typos... The second script is fine now (although I still need to change it more, but that's for later), however, the first one still has this problem, even after I made the array two-dimensional.
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    You still need to assign a starting value to the two dimensional array. Look at how you're doing it in the second script.
     
  6. Vini310

    Vini310

    Joined:
    Apr 22, 2020
    Posts:
    23
    Now I only need to fix the new "OutofBoundsArray" error. Naturally, this is happening because I added the wrong values in the array.

    EDIT: It's working fine now, and I can even properly test the scripts now, however I will left the thread open, because of something else involving the second script (not an error, just something I need to make).
     
    Last edited: May 5, 2020
  7. Vini310

    Vini310

    Joined:
    Apr 22, 2020
    Posts:
    23
    So, I have this script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MegaSenaV2 : MonoBehaviour
    6. {int i;
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.         int[,] megasena = new int[1001, 7];
    11.         int[] sorteio = new int[7];
    12.        
    13.         for (i = 1; i <=6; i++)
    14.         {
    15.             sorteio[i] = Random.Range(1, 60);
    16.         }
    17.        
    18.         System.Array.Sort(sorteio);
    19.        
    20.         sorteio[1] = 1;
    21.         sorteio[2] = 1;
    22.         for (i = 1; i <= 6; i++)
    23.         {
    24.             print("Numero " + i + " (" + sorteio[i] + ")");
    25.         }
    26.        
    27.         for (i = 2; i <=6; i++)
    28.         {
    29.             if (sorteio[i - 1] == sorteio[i])
    30.             {
    31.                 sorteio[i] = Random.Range(1, 60);
    32.                 System.Array.Sort(sorteio);
    33.                 i--;
    34.             }
    35.         }
    36.        
    37.         int n, limite = 10;
    38.         for (n = 1; n <= limite; n++)
    39.         {
    40.             for (i = 1; i <= 6; i++)
    41.             {
    42.                 megasena[n, 1] = sorteio[i];
    43.             }
    44.         }
    45.        
    46.         for (n = 1; n <= limite; n++)
    47.         {
    48.             print("Megasena [" + n +"] = " +
    49.             megasena[n, 1] + ", " +
    50.             megasena[n, 2] + ", " +
    51.             megasena[n, 3] + ", " +
    52.             megasena[n, 4] + ", " +
    53.             megasena[n, 5] + ", " +
    54.             megasena[n, 6]);
    55.         }
    56.     }
    57.  
    58.     // Update is called once per frame
    59.     void Update()
    60.     {
    61.        
    62.     }
    63. }
    64.  
    What this is supposed to do? Well:
    With this code, six random numbers between 1 and 60 will be generated on a matrix for the player, ten another matriz will be created (also randomly) as being the result of the raffle, however said matrix must be repeated 100 times, if the player wins, a victory message will be sent.

    Right now, the part of generating the six random numbers is incomplete, any testing only shows the first number, even after all the 10 attempts (29, 0, 0, 0, 0, 0).
     
  8. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Why are you never assigning a value, nor including in your Debug output, index 0 in any of your arrays?
     
  9. Vini310

    Vini310

    Joined:
    Apr 22, 2020
    Posts:
    23
    Since the last time I posted here, I changed many things in the script. For instance, on the part where the six random numbers appear, the n was changed to n.ToString. Also, two new strings were added: one for the values of the raffle and other for the "act" of raffling. Right now, it looks like this:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MegaSenaV2 : MonoBehaviour
    6. {      
    7.         public int i;
    8.         public int n;
    9.         public int[,] megasena = new int[1001, 7];
    10.         public int[] sorteio = new int[7];
    11.         public int[,] valores_sorteio = new int[61, 2];
    12.         public Dictionary<int, string> DadosMegaSena = new Dictionary<int, string>();
    13.         enum EmocaoApostador {Normal, Feliz, Realizado}
    14.         public int sortear()
    15.         {
    16.             for (i = 1; i <=6; i++)
    17.             {
    18.                 sorteio[i] = Random.Range(1, 61);
    19.                 valores_sorteio[sorteio[i], 1]++;
    20.             }
    21.        
    22.             System.Array.Sort(sorteio);
    23.        
    24.             sorteio[1] = 1;
    25.             sorteio[2] = 1;
    26.             for (i = 1; i <= 6; i++)
    27.             {
    28.                 print("Numero " + i + " (" + sorteio[i] + ")");
    29.             }
    30.        
    31.             for (i = 2; i <=6; i++)
    32.             {
    33.                 if (sorteio[i - 1] == sorteio[i])
    34.                 {
    35.                     valores_sorteio[sorteio[i], 1]--;
    36.                     sorteio[i] = Random.Range(1, 60);
    37.                     System.Array.Sort(sorteio);
    38.                     i--;
    39.                 }
    40.             }
    41.             return sorteio[6];
    42.         }
    43.         void Calcular_Chance(int[,] auxiliar)
    44.         {
    45.             int maior=0, menor=1000, medio=0, soma=0;
    46.             for(n=1; n<=60; n++)
    47.             {
    48.                 soma += auxiliar[n, 1];
    49.                 if(auxiliar[n, 1] > maior)
    50.                 {
    51.                     maior = auxiliar[n, 1];
    52.                 }
    53.                 if(auxiliar[n, 1] < menor)
    54.                 {
    55.                     menor = auxiliar[n, 1];
    56.                 }
    57.             }
    58.             medio = (int) (soma / 60);
    59.             print("Maior repetição = " + maior + " sorteios");
    60.             print("Menor repetição = " + menor + " sorteios");
    61.             print("Média = " + medio + " sorteios");
    62.             for (n = 1; n <=60; n++)
    63.             {
    64.                 if(auxiliar[n, 1] > (medio +10))
    65.                 {
    66.                     DadosMegaSena.Add(n, "alta");
    67.                 }
    68.                 else
    69.                 {
    70.                     if(auxiliar[n, 1] < (medio - 10))
    71.                     {
    72.                         DadosMegaSena.Add(n, "baixa");
    73.                     }
    74.                     else
    75.                     {
    76.                         DadosMegaSena.Add(n, "média");
    77.                     }  
    78.                 }
    79.             }
    80.             foreach(int chave in DadosMegaSena.Keys)
    81.             {
    82.                 print("o número \"" + chave + "\" tem uma chance " + DadosMegaSena[chave].ToUpper() + " de sair no sorteio da MegaSena.");
    83.             }
    84.         }
    85.        
    86.         void VerificarApostador()
    87.         {
    88.             int chance;
    89.             EmocaoApostador Jogador = EmocaoApostador.Normal;
    90.            
    91.             for(i = 1; i <=100; i++)
    92.             {
    93.                 chance = Random.Range(0, 100);
    94.                 if (chance >= 95)
    95.                 {
    96.                     Jogador = EmocaoApostador.Realizado;
    97.                 }
    98.                 else
    99.                 {
    100.                     if (chance >= 85)
    101.                     {
    102.                         Jogador = EmocaoApostador.Feliz;
    103.                     }
    104.                     print("Jogador " + i + " está" + Jogador + " ("+ chance + ")");
    105.                 }
    106.             }
    107.         }
    108.         void Start()
    109.         {
    110.             int n = 1, limite = 1000, valor = 0, mega = 1;
    111.             List<int> maiores = new List<int>();
    112.            
    113.             for (n = 1; n <= limite; n++)
    114.             {
    115.                 maiores.Add(sortear());
    116.                 for (i = 1; i <= 6; i++)
    117.                 {
    118.                     megasena[n, i] = sorteio[i];
    119.                 }
    120.             }
    121.            
    122.             foreach(int valores in maiores)
    123.             {
    124.                 print("Maior valor do sorteio " + mega++ + "=" + valores);
    125.             }
    126.             print(maiores.Count);
    127.            
    128.        
    129.             for (n = 1; n <= limite; n++)
    130.             {
    131.                 print("Megasena [" + n.ToString("0000") + "] = " +
    132.                 megasena[n, 1].ToString("00") + ", " +
    133.                 megasena[n, 2].ToString("00") + ", " +
    134.                 megasena[n, 3].ToString("00") + ", " +
    135.                 megasena[n, 4].ToString("00") + ", " +
    136.                 megasena[n, 5].ToString("00") + ", " +
    137.                 megasena[n, 6].ToString("00")
    138.                 );
    139.             }
    140.             for (n = 1; n <=60; n++)
    141.             {
    142.                 print("Número (" + n + ") = " + valores_sorteio[n, 1]);
    143.             }
    144.             Calcular_Chance(valores_sorteio);
    145.         }
    146.  
    147.     // Update is called once per frame
    148.     void Update()
    149.     {
    150.        
    151.     }
    152. }
    153.  
    It also indicates the chance each one of the numbers (the 60 of them) has of appearing ("low, medium, high") and another to show the player's reaction...

    This script was part of a class, but there's so many things happening in it I want to actually use it in a game I'm making, maybe to give rare items according to the number combination, but let's let that for another time...
     
  10. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I still have the same question:
    You're using arrays. Arrays start at index 0, not 1. You're never outputting the values at index 0 in any of your arrays, but you're sorting the arrays, which will include the values you're ignoring at index 0.
     
    bobisgod234 likes this.