- Home /
Expression denotes a `type', where a `variable', `value' or `method group' was expected
Hi all, I've seen a couple questions with this error. But I am not sure what exactly is going on. I'm pretty sure I have "news" in all the right spots but it just doesn't want to take it.
I'm using a 2D array (a 9x9 grid) to store a spawn points in a grid for a prefab. I also need to keep track if something is on in the grid, thus I made a custom class:
public class gridSpawn {
GameObject prefab;
public bool on;
Vector3 pos;
public gridSpawn( GameObject n, Vector3 b ){
prefab = n;
pos = b;
}
bool toggleOn(){ // toggles on, returns if val of on
on = !on;
return on;
}
void spawn(){
GameObject.Instantiate(prefab, pos, Quaternion.identity);
}
}
I'm setting up the grid in my start function in the main class for the script:
public class SpawnGrid : MonoBehaviour {
public GameObject pre = new GameObject();
private gridSpawn[,] grid;
// Use this for initialization
void Start () {
grid = new gridSpawn[9,9];
for( int i = 0; i < 9; i++ )
{
for( int z = 0; z < 9; z++ )
gridSpawn[i][z] = new gridSpawn( pre, new Vector3( i * 20 - 90 , z * -20 + 90, 80 ) );
}
}
// Update is called once per frame
void Update () {
}
}
The line in question is gridSpawn[i][z], I just don't see what I'm missing. The news are there, but it seems to suggest that pre somehow needs to be "new" yet it's a member of the class...
Anyway. I apologize for the formatting, it's durdling hard.
Answer by Kryptos · Jul 27, 2012 at 08:12 AM
You use a multidimensional array, but you are trying to access it as a jagged array.
Either change the declaration, or the way you access it:
/// multidimensional array
// declaration
private gridSpawn[,] grid;
// initialization
grid = new gridSpawn[9,9];
// usage
grid [i,j] = something;
/// jagged array
// declaration
private gridSpawn[][] grid;
// initialization
grid = new gridSpawn[9][];
for(int i=0; i<9; i++)
{
grid[i] = new int[9];
}
// usage
grid [i][j] = something;
Since you now excalty the size of your array, I would recommend the use of a multidimensional array which is faster and memory friendlier.
Note: about that last comment, multidimensional arrays should be faster than jagged array, but CLR implementation on Microsoft .Net is very bad. I don't know how this is implemented on Mono. But we can hope that this is better done.
Thanks, I also just realized another error with the code to.
$$anonymous$$y class was named gridSpawn and I durdled with the line of code: gridSpawn[i,z] = new gridSpawn(...);
It should have been: grid[i,z] = new gridSpawn(...);
I ran into issues with the multi vs. jagged right afterwards, so thanks!
Yes and in fact the error message was related to this error anyway. I should have been more careful.
As a general rule, you should start the name of your class with an Uppercase. This is not mandatory but Unity and .Net API do follow this rule (the only exception are for basic types such as bool or float, although there is a Uppercase version for each).
Your answer
![](https://koobas.hobune.stream/wayback/20220613074750im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Using WaitForSeconds and trigger 1 Answer
very funny problems,a function doesn't excute to the end 2 Answers