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));
}
}
}
}
}
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"
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.
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.