Error with code

hello all, i’m having some problem with my code. can anyone fix it? it says “A field initializer cannot reference the nonstatic field, method, or property `BlockSpawn.rnd’”
i am just a beginner on unity. thanks

using UnityEngine;
using System.Collections;

public class BlockSpawn : MonoBehaviour
{
public GameObject[ ] Sprite2_0;
public Vector3 spawnValues;
public int blockcount;
Random rnd = new Random();
int randomBlockNumber = rnd.Next(1, 6);
private int numberOfColumns = 33;
private int numberOfRows = 23;

void Start()
{
StartCouroutine(SpawnWaves());
}

IEnumerator SpawnWaves ()
{
while (true)
{
for (int i = 0; i < numberOfColumns; i++)
{
for (int a = 0; a < randomBlockNumber; a++)
{
Vector3 spawnPosition = new Vector3 (Random.Range (spawnValues.x * numberOfColumns, spawnValues.y * numberOfRows));
}
}
}
}
}

doesnt’ work.

int randomBlockNumber = rnd.Next(1, 6);

make

int randomBlockNumber;

and than

void Start()
{
randomBlockNumber = UnityEngine.Random.Range(1,6);
StartCouroutine(SpawnWaves());
}

i tried it and got “The type UnityEngine.Vector3' does not contain a constructor that takes 1’ arguments” and also "
The name `StartCouroutine’ does not exist in the current context"

Your vector 3 random is only bring back one number. A vector 3 needs 3 numbers seperated by commas.

“StartCoroutine” check spelling

new Vector3 (xValue, yValue, zValue)

what if it’s only for a 2D game, would i still need vector3

depends on how you build yor scene in unity.

but you would still need a second value for a Vector2

Out of curiosity have you downloaded & done the tutorials?

isn’t spawnValues.x and spawnValues.y the two values

You can use 2d & 3d stuff to build a 2d game. If you are using 2 points to determine where something is then it’s a vector2. You are creating a new vector3 which needs 3 numbers but all you are doing is creating a vector 3 that has the first number being a random number between those 2 values.

not in your code.

Vector3 spawnPosition = new Vector3 (Random.Range (spawnValues.x * numberOfColumns, spawnValues.y * numberOfRows));
Random.Range (spawnValues.x * numberOfColumns, spawnValues.y * numberOfRows)

Returns a random value between spawnValues.x * numberOfColumns and spawnValues.y * numberOfRows

so in the end you have, with your code,

Vector3 spawnPosition = new Vector3 (oneRandomIntValue);

why do you make Random.Range in your Vector, what do you intend?

to create random blocks in every column,

It won’t work that way, if the first random spot is x=1 then it will be put in 33(for example if your width is 33). If your grid is 30X30 & is located at 0,0,0 then you could try the x&y co-ords as a random between 0&31. You will need to do a while loop to check that it hasn’t spawned on top of another object that’s already placed. This will create x-blocks somewhere on the grid.

If you want to select from a number of items & place one of them on each x&y coordinate then the loop will be different as you increment along each x position up to the last one then increment the y position, reset x back to 0 & starte placing again. Repeat until you hit the y maximum as well.