Monodevelop from Unity says my variables don’t exist?

Asked

Viewed 788 times

1

I was following the fourth video tutorials of Roguelike scavengers, and I came across these errors when I finished. In the fifth video it fixes some, but these continued, I am using the latest version of Unity (5.1f1 I don’t know what else).

Erros no Unity

Variáveis não reconhecidas no Mono

GameObject tileChoice = tileArray(Random.Range(0,tileArray.Length));

He says that the 3 variables in red in the image do not exist in that context, and the other error he points to a line that appears to be normal.

Very good the videos that have excited me enough to start my project!

Here’s the whole code:

using UnityEngine;
using System; //Atributo Serializable - aplica variaveis no inspector e editor
using System.Collections.Generic; //Usar lists
using Random = UnityEngine.Random; //Gerar Numeros Aleatorios

public class BoardManager : MonoBehaviour { //padrao

    [Serializable] //tipo de variavel
    public class Count
    {
        public int minimum;
        public int maximum;

        public Count (int min, int max)
        {
            minimum = min;
            maximum = max;
        }
    }


    public int columns = 8; //Quantidade de colunas do mapa
    public int rows = 8; //Quantidade de Linhas do mapa
    public Count wallCount = new Count (5,9); //quantidade aleatoria de numeros internos
    public Count foodCount = new Count (1,5); //quantidade aleatoria de comida

    public GameObject exit; //objetos do jogo, q vao aparecer no mapa ->
    public GameObject [] floorTiles; 
    public GameObject [] wallTiles;
    public GameObject [] foodTiles;
    public GameObject [] enemyTiles;
    public GameObject [] outerWallTiles; // <- 

    private Transform boardHolder; //segura tds os objetos no mapa
    private List <Vector2> gridPosition = new List<Vector2> (); //lista de posiscoes possiveis no mapa

    void initialiseList () //limpar a lista do grid e preparar para gerar novo mapa
    {
        gridPosition.Clear (); //limpa o mapa
        for (int x=1; x<columns-1; x++) //loop navega pelas colunas
        {
            for (int y=1; y<rows-1; y++)
            {
                gridPosition.Add (new Vector2(x,y));
            }

        }
    }

    void BoardSetup ()
    {
        //starta o mapa e atribui o transform
        boardHolder = new GameObject ("Board").transform;
        for (int x=-1; x<columns+1; x++) 
        {
            for (int y=-1; y<rows+1; y++)
            {
                //seleciona um tile para aplicar no mapa
                GameObject toInstantiate = floorTiles(Random.Range(0,floorTiles.Length));

                //verifica se e muro externo
                if (x == -1 || y == -1 || x == columns || y == rows)
                {
                    toInstantiate = outerWallTiles(Random.Range(0,outerWallTiles.Length));
                }

                GameObject instance = Instantiate (toInstantiate, new Vector2(x,y), Quaternion.identity) as GameObject;
                instance.transform.SetParent (boardHolder);

            }
        }
    }

    //retorna valor para grid position
    Vector2 RandomPosition()
    {
        int randomIndex = Random.Range (0, gridPosition.Count);

        Vector2 randomPosition = gridPosition (randomIndex);

        gridPosition.RemoveAt (randomIndex);

        return randomPosition;
    }

    void LayoutObjectAtRandom(GameObject[] tileArray, int minimum, int maximum)
    {
        int objectCount = Random.Range (minimum, maximum);

        for (int i = 0; i < objectCount; i++) 
        {
            Vector3 randomPosition = RandomPosition();

            GameObject tileChoice = tileArray(Random.Range(0,tileArray.Length));
            Instantiate(tileChoice, randomPosition, Quaternion.identity);
        }
    }

    public void SetupScene (int Level)
    {
        BoardSetup (); //starta o mapa

        initialiseList (); //starta o grid

        //instancia numeros aleatorios de muros internos
        LayoutObjectAtRandom (wallTiles, wallCount.minimum, wallCount.maximum);
        //instancia o numero aleatorio de comida
        LayoutObjectAtRandom (foodTiles, foodCount.minimum, foodCount.maximum);

        //seta o numero de enemys baseado no lvl
        int enemyCount = (int)Mathf.Log (Level, 2f);
        LayoutObjectAtRandom (enemyTiles, enemyCount, enemyCount);

        //
        Instantiate (exit, new Vector2 (columns-1, rows-1), Quaternion.identity);
    }

}
  • 4

    Who is Nils? What videos? This seems to be a question directed to a specific person. And the Stack Overflow is a community! If you are looking for help from a certain person, it would be best to contact them directly through other means. But if you’re looking for help from the community, then you should edit and reshape your question so that it’s directed to the community and not just one person, so we can help you.

  • https://www.youtube.com/playlist?list=PLa2bQ5uCFWA15RYHCnhjSbf_LiC4eFEQ0 this is nills !! He teaches to make games and recommended to ask questions here !! kkkkk mals you’re right, I went to think about it later only and left the question so msm, pardon !!

1 answer

5


I’m not Nils, but I’ll answer your question. This is a community where there are thousands of users responding, and Nils is just one of many. Because of this you should direct your question to everyone and not just to him.

But anyway, come on:


Those are arrays:

public GameObject [] floorTiles;
public GameObject [] outerWallTiles;

That is a list:

private List <Vector2> gridPosition = new List<Vector2> (); //lista de posiscoes possiveis no mapa

And this is an array as parameter:

void LayoutObjectAtRandom(GameObject[] tileArray, int minimum, int maximum)

These are attempts to call methods:

GameObject toInstantiate = floorTiles(Random.Range(0,floorTiles.Length));
// ...
toInstantiate = outerWallTiles(Random.Range(0,outerWallTiles.Length));
// ...
Vector2 randomPosition = gridPosition (randomIndex);
// ...
GameObject tileChoice = tileArray(Random.Range(0,tileArray.Length));

However floorTiles, outerWallTiles, gridPosition and tileArray are not methods, but collections of objects.

I think what you wanted was this:

GameObject toInstantiate = floorTiles[Random.Range(0, floorTiles.Length)];
// ...
toInstantiate = outerWallTiles[Random.Range(0, outerWallTiles.Length)];
// ...
Vector2 randomPosition = gridPosition[randomIndex];
// ...
GameObject tileChoice = tileArray[Random.Range(0, tileArray.Length)];

I mean, you used parentheses () instead of square brackets [].

  • Using parentheses after a variable name means trying to invoke a method with the variable name.
  • Using square brackets after an expression that results in an object (and the name of a variable is an expression) means trying to access a position of the object that must be a list or array.
  • Thank you very much man, I’ll try here !! kkkkkk yes I thought that was going to be directed straight to it !! I thought better then just... I’ll test here !! I don’t know anything about c#... I think besides following the video lessons I will follow a booklet part tbm to better understand the syntax. ?

  • @Pedrof.Neto Is there a reason why you rejected the acceptance of the answer after so long? Something is wrong here?

  • this week after a long time I went back to messing with this code and gridPosition is still giving the same error, its solution solved some things but not the whole problem. I will seek to learn the syntax of the right language

  • @Pedrof.Neto If your new problem is similar to this one, I think you will not have much difficulty finding the solution by applying what you have here in your new problem. But if your new problem is quite different, then I recommend you ask a new question, because this one is already very old.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.