I'm making a simple Tetris game in Unity. Everything works except I can't clear lines and I don't know why.
In my code I put in to check for completed lines. If it is a completed line it should clear the line and all the blocks should move one row down. Unfortunately, my code doesn't work.
public Vector3 rotationPoint;
private float previousTime;
public float fallTime = 0.8f;
public static int height = 20;
public static int width = 10;
private static Transform[,] grid = new Transform[width, height];
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.LeftArrow)) {
transform.position += new Vector3 (-1, 0, 0);
if (!ValidMove ())
transform.position -= new Vector3 (-1, 0, 0);
} else if (Input.GetKeyDown (KeyCode.RightArrow)) {
transform.position += new Vector3 (1, 0, 0);
if (!ValidMove ())
transform.position -= new Vector3 (1, 0, 0);
} else if (Input.GetKeyDown (KeyCode.UpArrow)) {
transform.RotateAround (transform.TransformPoint(rotationPoint), new Vector3 (0, 0, 1), 90);
if (!ValidMove ())
transform.RotateAround (transform.TransformPoint(rotationPoint), new Vector3 (0, 0, 1), -90);
}
if (Time.time - previousTime > (Input.GetKey (KeyCode.DownArrow) ? fallTime / 10 : fallTime)) {
transform.position += new Vector3 (0, -1, 0);
if (!ValidMove ()){
transform.position -= new Vector3 (0, -1, 0);
AddToGrid ();
this.enabled = false;
FindObjectOfType<SpawnTetromino> ().NewTetromino ();
}
previousTime = Time.time;
}
}
void CheckForLines(){
for (int i = height-1; i >= 0; i--){
if(HasLine(i)){
DeleteLine(i);
RowDown(i);
}
}
}
bool HasLine(int i){
for(int j = 0; j< width; j++){
if(grid[j, i] == null)
return false;
}
return true;
}
void DeleteLine(int i){
for (int j = 0; j < width; j++){
Destroy(grid[j, i].gameObject);
grid[j, i] = null;
}
}
void RowDown(int i){
for (int y = i; y < height; y++){
for (int j = 0; j < width; j++){
if(grid[j,y] != null){
grid[j, y - 1] = grid[j,y];
grid[j, y] = null;
grid[j, y - 1].transform.position -= new Vector3(0, 1, 0);
}
}
}
}
void AddToGrid(){
foreach (Transform children in transform) {
int roundedX = Mathf.RoundToInt (children.transform.position.x);
int roundedY = Mathf.RoundToInt (children.transform.position.y);
grid [roundedX, roundedY] = children;
}
}
bool ValidMove(){
foreach (Transform children in transform) {
int roundedX = Mathf.RoundToInt (children.transform.position.x);
int roundedY = Mathf.RoundToInt (children.transform.position.y);
if (roundedX < 0 || roundedX >= width || roundedY < 0 || roundedY >= height) {
return false;
}
if (grid [roundedX, roundedY] != null)
return false;
}
return true;
}
thanks in advance.
Add CheckForLines() to the end of Update method. Currently you are not using it
Related
I decided to make my own A*. When I enable this part of the code Unity crashes.
if (Physics2D.Raycast(transform.position, a - new Vector2(current.x, current.y), 1, mask))
{
print(i);
continue;
}
There are no other objects than walls that would enable the Raycast.
The code probably enters an infinite loop, but I have no idea how because it works fine if i disable raycasts.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEditor;
using UnityEngine;
public class Star : MonoBehaviour
{
private int x, y;
private int nextX, nextY;
public Vector2 targetNode;
private Vector2 nextNode;
private List<Node> path = null;
LayerMask mask;
Bounds area;
private bool moving;
private bool check;
public float timeStart;
public Vector3 startPos;
public float speed = 1f;
float u;
Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
mask = LayerMask.GetMask("Default");
}
void FixedUpdate()
{
if (!moving)
{
Move();
}
else
MoveTo(speed);
}
private void Move()
{
area = new Bounds(transform.position, new Vector3(100, 100));
int playerX = (int)Mathf.Round(Player.x);
int playerY = (int)Mathf.Round(Player.y);
Vector2 start = new Vector2((int)transform.position.x, (int)transform.position.y);
Vector2 destination = new Vector2(playerX, playerY);
if (getDistance(start, destination) > 1f)
path = findPath(start, destination);
else
path = null;
nextX = 0;
nextY = 0;
if (path != null)
{
if (path.Count > 0)
{
nextNode = new Vector2(path[path.Count - 1].x, path[path.Count - 1].y);
if (x < nextNode.x) nextX++;
if (x > nextNode.x) nextX--;
if (y < nextNode.y) nextY++;
if (y > nextNode.y) nextY--;
}
}
if (nextX != 0 || nextY != 0)
{
Move(nextX, nextY);
}
}
public List<Node> findPath(Vector2 start, Vector2 goal)
{
x = (int)start.x;
y = (int)start.y;
List<Node> openList = new List<Node>();
List<Node> closedList = new List<Node>();
Node current = new Node(x, y, null, 0, getDistance(start, goal));
openList.Add(current);
while (openList.Count > 0)
{
openList.Sort();
current = openList[0];
if (new Vector2(current.x, current.y).Equals(goal))
{
List<Node> path = new List<Node>();
while (current.parent != null)
{
path.Add(current);
current = current.parent;
}
openList.Clear();
closedList.Clear();
return path;
}
openList.Remove(current);
closedList.Add(current);
for (int i = 0; i < 9; i++)
{
if (i == 4/* || i == 0 || i == 2 || i == 6 || i == 8*/) continue;
int x = current.x;
int y = current.y;
int xi = (i % 3) - 1; // -1, 0, 1
int yi = (i / 3) - 1;
Vector2 a = new Vector2(x + xi, y + yi);
if (Physics2D.Raycast(transform.position, a - new Vector2(current.x, current.y), 1, mask))
{
print(i);
continue;
}
double gCost = current.gCost + (getDistance(new Vector2(current.x, current.y), a));
double hCost = getDistance(a, goal);
Node node = new Node((int)a.x, (int)a.y, current, gCost, hCost); // store the node
if (!vecInList(openList, a)) openList.Add(node);
}
}
closedList.Clear();
return null;
}
private double getDistance(Vector2 tile, Vector2 goal)
{
int dx = (int)(tile.x - goal.x);
int dy = (int)(tile.y - goal.y);
return Mathf.Sqrt((dx * dx) + (dy * dy));
}
private bool vecInList(List<Node> list, Vector2 vector)
{
foreach (Node n in list)
{
Vector2 tile2 = new Vector2(n.x, n.y);
if (tile2.Equals(vector)) return true;
}
return false;
}
public void Move(int x, int y)
{
targetNode = transform.position + new Vector3(x, y);
if (area.Contains(new Vector3(Player.x, Player.y)) && (moving == false))
{
moving = true;
check = true;
}
}
public void MoveTo(float speed)
{
if (check)
{
check = false;
moving = true;
timeStart = Time.time;
startPos = transform.position;
}
if (moving)
{
u = (Time.time - timeStart) / (1 / speed);
if (u >= 1)
{
u = 1;
moving = false;
}
//transform.position = (1 - u) * (Vector2)startPos + u * targetNode;
rb.MovePosition((1 - u) * (Vector2)startPos + u * targetNode);
}
}
}
Edit: got this to work thanks to Yuri's comment. Changed to:
if (Physics2D.Raycast(new Vector2(current.x, current.y), a - new Vector2(current.x, current.y), 1, mask))
I was checking from the start position instead of the last position in the nodes.
I'm making a total war demo game where you drag on the screen to move your units. This bug has been bothering me and I don't know what's the reason behind it. The error I get is below.
FormationScript.cs(119,44): error CS1503: Argument 1: cannot convert from 'UnityEngine.Vector3' to 'System.Collections.Generic.List<UnityEngine.Vector3>'
Here is the code. (sorry for its length)
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Debug = UnityEngine.Debug;
public class FormationScript : MonoBehaviour
{
public GameObject[] units;
public Transform formationBarrier;
float unitWidth = 2.0f; // This also includes the gap between them, in this case the unit width is 1 and the gap is also 1
private float numberOfUnits;
private double unitsPerRow;
private float numberOfRows;
Vector3 startClick;
Vector3 endClick;
Vector3 selectedAreaSize;
Vector3 selectedAreaPos;
float startMinusEndX;
float startMinusEndZ;
private List<List<Vector3>> availablePositions;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Formation();
}
void Formation()
{
if (Input.GetMouseButtonDown(1))
{
Plane plane = new Plane(Vector3.up, 0);
float distance;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (plane.Raycast(ray, out distance))
{
startClick = ray.GetPoint(distance);
}
Debug.Log(startClick);
}
if (Input.GetMouseButtonUp(1))
{
Plane plane = new Plane(Vector3.up, 0);
float distance;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (plane.Raycast(ray, out distance))
{
endClick = ray.GetPoint(distance);
}
Debug.Log(endClick);
}
// ======================================================================================================
/*if (startClick.x - endClick.x < 0 || startClick.z - endClick.z < 0)
{
startMinusEndX = startClick.x + endClick.x;
startMinusEndZ = startClick.z + endClick.z;
}
else
{
startMinusEndX = startClick.x - endClick.x;
startMinusEndZ = startClick.z - endClick.z;
}
if (startMinusEndX > 0 && startMinusEndZ > 0)
{
formationBarrier.localScale = new Vector3(startMinusEndX, 1, startMinusEndZ);
formationBarrier.localPosition = new Vector3((startClick.x + endClick.z) / 2, 0, (startClick.z + endClick.z) / 2);
}
else if (startMinusEndX < 0 && startMinusEndZ > 0)
{
formationBarrier.localScale = new Vector3(startMinusEndX, 1, startMinusEndZ);
formationBarrier.localPosition = new Vector3((startClick.x + endClick.z) * 2, 0, (startClick.z + endClick.z) / 2);
}
*/
startMinusEndX = startClick.x - endClick.x;
startMinusEndZ = startClick.z - endClick.z;
formationBarrier.localScale = new Vector3(startMinusEndX, 1, startMinusEndZ);
formationBarrier.localPosition = new Vector3((startClick.x + endClick.z) / 2, 0, (startClick.z + endClick.z) / 2);
// ======================================================================================================
selectedAreaSize = formationBarrier.localScale;
selectedAreaPos = formationBarrier.localPosition;
numberOfUnits = units.Length;
unitsPerRow = (unitWidth / numberOfUnits) * selectedAreaSize.x;
unitsPerRow = Math.Round(unitsPerRow, 0);
if (unitsPerRow < 0)
{
unitsPerRow = unitsPerRow * -2;
}
if (numberOfRows % 1 == 0)
{
numberOfRows = numberOfUnits % (float)unitsPerRow;
for (int i = 0; i > numberOfRows; i++) // i is the number of rows
{
//availablePositions.Add(i);
for (int j = 0; j > numberOfUnits / unitsPerRow; j++) // j is the number of units / the units per row
{
Vector3 pos = new Vector3((selectedAreaPos.x / ((float)unitsPerRow + 1)) + ((j - 1) * (selectedAreaPos.x / ((float)unitsPerRow + 1))), 0.0f, i * 2);
availablePositions.Add(pos); // Heres where the error's coming from
}
}
}
else if (numberOfUnits % (float)unitsPerRow != 0)
{
numberOfRows = numberOfUnits % (float)unitsPerRow;
}
Debug.Log(unitsPerRow);
Debug.Log(numberOfRows);
}
}
I am pretty new to Unity so go easy : )
There is a syntax error in your code. You are inserting pos which is of type Vector3 into availablePositions, which is a List of List of Vecotr3.
Either change availablePositions definition:
private List<Vector3> availablePositions;
or convert pos to list before adding to availablePositions:
availablePositions.Add(new List<Vector3>{pos});
I need help with trajectory prediction on mobile.
I'm able to do that on PC using the mouse, but I'm not able to do that with touch functions on mobile.
I've the dot that I copy on the start, and while I'm dragging the ball I want to calculate my trajectory.
After touch will end I will destroy these dots.
As I said I'm able to do it using Input.GetMouseButton/Up/Down but I'm not able to do that using touch function.
Here is my code:
private void Update()
{
//if (Input.touchCount > 0)
//{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
startPosition = Input.GetTouch(0).position;
for (int i = 0; i < number; i++)
{
trajectoryDots[i] = Instantiate(trajectoryDot, gameObject.transform);
}
}
if (Input.touchCount > 0)
{
for (int i = 0; i < number; i++)
{
trajectoryDots[i].transform.position = calculatePosition(i * 0.1f);
}
}
if (touch.phase == TouchPhase.Ended)
{
for (int i = 0; i < number; i++)
{
Destroy(trajectoryDots[i]);
}
endPoisiton = Input.GetTouch(0).position;
force = startPosition - endPoisiton;
ballRigid.gravityScale = 1;
ballRigid.velocity = new Vector2(force.x * power, force.y * power);
}
//}
}
private Vector2 calculatePosition(float elapsedTime)
{
return new Vector2(endPoisiton.x, endPoisiton.y) +
new Vector2(force.x * power, force.y * power) * elapsedTime +
0.5f * Physics2D.gravity * elapsedTime * elapsedTime;
}
The code I used for mouse input
if (Input.GetMouseButtonDown(0))
{ //click
startPos = gameObject.transform.position;
for (int i = 0; i < number; i++)
{
trajectoryDots[i] = Instantiate(trajectoryDot, gameObject.transform);
}
}
if (Input.GetMouseButton(0))
{ //drag
endPos = Camera.main.ScreenToWorldPoint(Input.mousePosition) + new Vector3(0, 0, 10);
gameObject.transform.position = endPos;
forceAtPlayer = endPos - startPos;
for (int i = 0; i < number; i++)
{
trajectoryDots[i].transform.position = calculatePosition(i * 0.1f);
}
}
if (Input.GetMouseButtonUp(0))
{ //leave
rigidbody.gravityScale = 1;
rigidbody.velocity = new Vector2(-forceAtPlayer.x * forceFactor, -forceAtPlayer.y * forceFactor);
for (int i = 0; i < number; i++)
{
Destroy(trajectoryDots[i]);
}
}
So after little changes it's much better, I' am getting closer to what i want there are still some glitches i guess because of touch.moved so when I' am on display with my finger and do only little moves ball is jumping on the screen
Here is the code
private void Update()
{
if (Input.touchCount > 0)
{
var touch = Input.GetTouch(0);
switch (touch.phase)
{
case TouchPhase.Began:
initPosition = gameObject.transform.position;
startPosition = cam.ScreenToWorldPoint(touch.position) + new Vector3(0, 0, 10);
for (int i = 0; i < number; i++)
{
trajectoryDots[i] = Instantiate(trajectoryDot, gameObject.transform);
trajectoryDots[i].transform.position = calculatePosition(i * 0.1f);
}
break;
case TouchPhase.Moved:
endPosition = Camera.main.ScreenToWorldPoint(touch.position) + new Vector3(0, 0, 10);
gameObject.transform.position = initPosition;
force = startPosition - endPosition;
for (int i = 0; i < number; i++)
{
trajectoryDots[i].transform.position = calculatePosition(i * 0.1f);
}
break;
case TouchPhase.Ended:
ballRigid.gravityScale = 1;
ballRigid.velocity = new Vector2(force.x * power, force.y * power);
for (int i = 0; i < number; i++)
{
Destroy(trajectoryDots[i]);
}
break;
}
}
}
private Vector2 calculatePosition(float elapsedTime)
{
return new Vector2(initPosition.x, initPosition.y) +
new Vector2(force.x * power, force.y * power) * elapsedTime +
0.5f * Physics2D.gravity * elapsedTime * elapsedTime;
}
Ok i have final soution and it's working correctly
private void Update()
{
if (Input.touchCount > 0)
{
var touch = Input.GetTouch(0);
switch (touch.phase)
{
case TouchPhase.Began:
initPosition = gameObject.transform.position;
startPosition = cam.ScreenToWorldPoint(touch.position) + new Vector3(0, 0, 10);
for (int i = 0; i < number; i++)
{
trajectoryDots[i] = Instantiate(trajectoryDot, gameObject.transform);
trajectoryDots[i].transform.position = calculatePosition(i * 0.1f);
}
break;
case TouchPhase.Ended:
ballRigid.gravityScale = 1;
ballRigid.velocity = new Vector2(force.x * power, force.y * power);
for (int i = 0; i < number; i++)
{
Destroy(trajectoryDots[i]);
}
break;
}
endPosition = Camera.main.ScreenToWorldPoint(touch.position) + new Vector3(0, 0, 10);
gameObject.transform.position = initPosition;
force = startPosition - endPosition;
for (int i = 0; i < number; i++)
{
trajectoryDots[i].transform.position = calculatePosition(i * 0.1f);
}
}
}
private Vector2 calculatePosition(float elapsedTime)
{
return new Vector2(initPosition.x, initPosition.y) +
new Vector2(force.x * power, force.y * power) * elapsedTime +
0.5f * Physics2D.gravity * elapsedTime * elapsedTime;
}
So just taking your mouse code and convert it to touch code it might look like
if(Input.touchCount > 0)
{
var touch = Input.GetTouch(0);
switch(touch.phase)
{
case TouchPhase.Began:
// Note: to be more accurate you actually probably would
// rather also here use ScreenToWorldPoint on the touch.position
// same way as later. Otherwise you might allways get a force even if touch wasn't moved at all
//startPos = transform.position;
startPos = Camera.main.ScreenToWorldPoint(touch.position) + new Vector3(0, 0, 10);
for (int i = 0; i < number; i++)
{
trajectoryDots[i] = Instantiate(trajectoryDot, gameObject.transform);
trajectoryDots[i].transform.position = calculatePosition(i * 0.1f);
}
break;
case TouchPhase.Moved:
// These were missing in your touch version
// You did it only in the end so there were no preview trajectory
// And note that also touch is in pixel screenspace so if you
// want to use world positions you need to convert them just the same as for mouse input!
endPos = Camera.main.ScreenToWorldPoint(touch.position) + new Vector3(0, 0, 10);
transform.position = endPos;
force = endPos - startPos;
for (int i = 0; i < number; i++)
{
trajectoryDots[i].transform.position = calculatePosition(i * 0.1f);
}
break;
case TouchPhase.Ended:
rigidbody.gravityScale = 1;
// Not sure why but in your mouse code you used a negative force here
// but in this case your trajectory would have been wrong since there you didn't use negative values...
rigidbody.velocity = force * forceFactor;
for (int i = 0; i < number; i++)
{
Destroy(trajectoryDots[i]);
}
break;
}
}
Btw the calculate method gets better to read like
private Vector2 calculatePosition(float elapsedTime)
{
return endPoisiton
+ force * power * elapsedTime
+ Physics2D.gravity * 0.5f * elapsedTime * elapsedTime;
}
Note: Typed on smartphone but I hope the idea gets clear
private void MoveToNewFormation()
{
squadMembers = GameObject.FindGameObjectsWithTag("Squad Member");
float step = speed * Time.deltaTime;
for (int i = 0; i < squadMembers.Length; i++)
{
squadMembers[i].transform.LookAt(newpos[i]);
squadMembers[i].transform.position = Vector3.MoveTowards(squadMembers[i].transform.position, newpos[i], step);
//squadMembers[i].transform.rotation = qua[i];
}
}
And calling it in the Update:
void Update()
{
if (Input.GetKeyDown(KeyCode.F))
{
ChangeFormation();
}
if (move == true)
MoveToNewFormation();
}
Once when one of the squadMembers reached to the newpos then i want to make
squadMembers[i].transform.rotation = qua[i];
qua is a List and i want to rotate the squad member once he reached the newpos.
Inside MoveToNewFormation i thought to add after the loop the line:
if (squadMembers[i].transform.position == newpos[i])
{
squadMembers[i].transform.rotation = qua[i];
}
But it's after the loop so 'i' not exist.
This is the complete script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SquadFormation : MonoBehaviour
{
enum Formation
{
Square, Circle, Triangle
}
public Transform squadMemeber;
public int columns = 4;
public int squareSpace = 10;
public int circleSpace = 40;
public int numberOfObjects = 20;
public float yOffset = 0;
public float speed = 3;
private Formation formation;
private GameObject[] squadMembers;
private List<Quaternion> qua = new List<Quaternion>();
private List<Vector3> newpos = new List<Vector3>();
private bool move = false;
// Use this for initialization
void Start()
{
formation = Formation.Square;
ChangeFormation();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.F))
{
ChangeFormation();
}
if (move == true)
MoveToNewFormation();
}
private void ChangeFormation()
{
switch (formation)
{
case Formation.Square:
FormationSquare();
break;
case Formation.Circle:
FormationCircle();
break;
}
}
private Vector3 FormationSquarePositionCalculation(int index) // call this func for all your objects
{
float posX = (index % columns) * squareSpace;
float posY = (index / columns) * squareSpace;
return new Vector3(posX, posY);
}
private void FormationSquare()
{
for (int i = 0; i < numberOfObjects; i++)
{
Transform go = Instantiate(squadMemeber);
Vector3 pos = FormationSquarePositionCalculation(i);
go.position = new Vector3(transform.position.x + pos.x, 0, transform.position.y + pos.y);
go.Rotate(new Vector3(0, -90, 0));
go.tag = "Squad Member";
}
formation = Formation.Circle;
}
private Vector3 FormationCirclePositionCalculation(Vector3 center, float radius, int index, float angleIncrement)
{
float ang = index * angleIncrement;
Vector3 pos;
pos.x = center.x + radius * Mathf.Sin(ang * Mathf.Deg2Rad);
pos.z = center.z + radius * Mathf.Cos(ang * Mathf.Deg2Rad);
pos.y = center.y;
return pos;
}
private void FormationCircle()
{
Vector3 center = transform.position;
float radius = (float)circleSpace / 2;
float angleIncrement = 360 / (float)numberOfObjects;
for (int i = 0; i < numberOfObjects; i++)
{
Vector3 pos = FormationCirclePositionCalculation(center, radius, i, angleIncrement);
var rot = Quaternion.LookRotation(center - pos);
pos.y = Terrain.activeTerrain.SampleHeight(pos);
pos.y = pos.y + yOffset;
newpos.Add(pos);
qua.Add(rot);
}
move = true;
formation = Formation.Square;
}
private void MoveToNewFormation()
{
squadMembers = GameObject.FindGameObjectsWithTag("Squad Member");
float step = speed * Time.deltaTime;
for (int i = 0; i < squadMembers.Length; i++)
{
squadMembers[i].transform.LookAt(newpos[i]);
squadMembers[i].transform.position = Vector3.MoveTowards(squadMembers[i].transform.position, newpos[i], step);
//squadMembers[i].transform.rotation = qua[i];
}
//if (squadMembers[i].transform.position == newpos[i])
}
}
You can use a threshold and check the distance using that threshold.
Define threshold like this at start of script.
public float threshold = 0.1f;
Then change the MoveToNewFormation() function like this:
private void MoveToNewFormation()
{
squadMembers = GameObject.FindGameObjectsWithTag("Squad Member");
float step = speed * Time.deltaTime;
for (int i = 0; i < squadMembers.Length; i++)
{
squadMembers[i].transform.LookAt(newpos[i]);
squadMembers[i].transform.position =
Vector3.MoveTowards(squadMembers[i].transform.position, newpos[i], step);
if(Vector3.Distance(squadMembers[i].transform.position,newpos[i])<threshold){
squadMembers[i].transform.rotation = qua[i];
}
}
//if (squadMembers[i].transform.position == newpos[i])
}
I have made a game in unity which have multiple scenes of different level in which user have to select a colored block to go the next level but i want my code to be in a single scene is there a way i can convert my multi scene game into a single scene .
Code for level 1 grid base is
public IEnumerator Start() {
grid = new GameObject[ySize, xSize];
float x = xStart;
float y = yStart;
ScreenRandom = Random.Range(0, 3);
if (ScreenRandom == 0)
{
img.color = UnityEngine.Color.red;
text.text = "Select all the Red Objects";
yield return new WaitForSeconds(time);
Destroy(img);
Destroy(text);
int check = 1;
for (int i = 0; i < ySize; i++)
{
for (int j = 0; j < xSize; j++)
{
if (check <= 1)
{
GameObject rblock = Instantiate(RedPrefabs[Random.Range(0, 1)]) as GameObject;
rblock.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
rblock.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
rblock.transform.SetParent(canvas.transform);
grid[i, j] = rblock;
CountRed++;
}
else {
GameObject block = Instantiate(NonRedPrefab[Random.Range(0, 2)]) as GameObject;
block.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
block.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
block.transform.SetParent(canvas.transform);
grid[i, j] = block;
}
check++;
x += xWidth * space;
}
y -= yHeight * space;
x = xStart;
}
}
if (ScreenRandom == 1)
{
img.color = UnityEngine.Color.blue;
text.text = "Select all the Blue Objects";
yield return new WaitForSeconds(time);
Destroy(img);
Destroy(text);
int check = 1;
for (int i = 0; i < ySize; i++)
{
for (int j = 0; j < xSize; j++)
{
if (check <= 1)
{
GameObject rblock = Instantiate(BluePrefabs[Random.Range(0, 1)]) as GameObject;
rblock.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
rblock.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
rblock.transform.SetParent(canvas.transform);
grid[i, j] = rblock;
CountBlue++;
}
else {
GameObject block = Instantiate(NonBluePrefab[Random.Range(0, 2)]) as GameObject;
block.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
block.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
block.transform.SetParent(canvas.transform);
grid[i, j] = block;
}
check++;
x += xWidth * space;
}
y -= yHeight * space;
x = xStart;
}
}
if (ScreenRandom == 2)
{
img.color = UnityEngine.Color.yellow;
text.text = "Select all the Yellow Objects";
yield return new WaitForSeconds(time);
Destroy(img);
Destroy(text);
int check = 1;
for (int i = 0; i < ySize; i++)
{
for (int j = 0; j < xSize; j++)
{
if (check <= 1)
{
GameObject rblock = Instantiate(YellowPrefabs[Random.Range(0, 1)]) as GameObject;
rblock.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
rblock.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
rblock.transform.SetParent(canvas.transform);
grid[i, j] = rblock;
CountYellow++;
}
else {
GameObject block = Instantiate(NonYellowPrefab[Random.Range(0, 2)]) as GameObject;
block.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
block.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
block.transform.SetParent(canvas.transform);
grid[i, j] = block;
}
check++;
x += xWidth * space;
}
y -= yHeight * space;
x = xStart;
}
}
}
Code to check and Load Level 2 is:
void Update () {
screen = GridControl.ScreenRandom;
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (screen == 0)
{
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.tag == "BlueBlock")
{
Destroy(hit.transform.gameObject);
print("GameOver");
Application.LoadLevel(0);
}
if (hit.collider.tag == "RedBlock")
{
RedCount++;
Destroy(hit.transform.gameObject);
Correct++;
Score.text = " " + Correct;
if (RedCount == GridControl.CountRed)
{
print("Next Level");
Application.LoadLevel(3);
}
}
if (hit.collider.tag == "YellowBlock")
{
Destroy(hit.transform.gameObject);
print("GameOver");
Application.LoadLevel(0);
}
}
}
if (screen == 1)
{
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.tag == "BlueBlock")
{
BlueCount++;
Destroy(hit.transform.gameObject);
Correct++;
Score.text = " " + Correct;
if (BlueCount == GridControl.CountBlue)
{
print("Next Level");
Application.LoadLevel(3);
}
}
if (hit.collider.tag == "RedBlock")
{
Destroy(hit.transform.gameObject);
print("GameOver");
Application.LoadLevel(0);
}
if (hit.collider.tag == "YellowBlock")
{
Destroy(hit.transform.gameObject);
print("GameOver");
Application.LoadLevel(0);
}
}
}
if (screen == 2)
{
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.tag == "BlueBlock")
{
Destroy(hit.transform.gameObject);
print("GameOver");
Application.LoadLevel(0);
}
if (hit.collider.tag == "RedBlock")
{
Destroy(hit.transform.gameObject);
print("Game Over");
Application.LoadLevel(0);
}
if (hit.collider.tag == "YellowBlock")
{
YellowCount++;
Destroy(hit.transform.gameObject);
Correct++;
Score.text = " " + Correct;
if (YellowCount == GridControl.CountYellow)
{
print("Next Level");
Application.LoadLevel(3);
}
}
}
Code for Level 2 GridBase is;
public void Start()
{
grid = new GameObject[ySize, xSize];
ScreenRandom = GridControl.ScreenRandom;
float x = xStart;
float y = yStart;
if (ScreenRandom == 0)
{
int check = 1;
for (int i = 0; i < ySize; i++)
{
for (int j = 0; j < xSize; j++)
{
if (check <= 2)
{
GameObject rblock = Instantiate(RedPrefabs[Random.Range(0, 1)]) as GameObject;
rblock.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
rblock.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
rblock.transform.SetParent(canvas.transform);
grid[i, j] = rblock;
CountRed++;
}
else {
GameObject block = Instantiate(NonRedPrefab[Random.Range(0, 2)]) as GameObject;
block.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
block.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
block.transform.SetParent(canvas.transform);
grid[i, j] = block;
}
check++;
x += xWidth * space;
}
y -= yHeight * space;
x = xStart;
}
}
if (ScreenRandom == 1)
{
int check = 1;
for (int i = 0; i < ySize; i++)
{
for (int j = 0; j < xSize; j++)
{
if (check <= 2)
{
GameObject rblock = Instantiate(BluePrefabs[Random.Range(0, 1)]) as GameObject;
rblock.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
rblock.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
rblock.transform.SetParent(canvas.transform);
grid[i, j] = rblock;
CountBlue++;
}
else {
GameObject block = Instantiate(NonBluePrefab[Random.Range(0, 2)]) as GameObject;
block.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
block.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
block.transform.SetParent(canvas.transform);
grid[i, j] = block;
}
check++;
x += xWidth * space;
}
y -= yHeight * space;
x = xStart;
}
}
if (ScreenRandom == 2)
{
int check = 1;
for (int i = 0; i < ySize; i++)
{
for (int j = 0; j < xSize; j++)
{
if (check <= 2)
{
GameObject rblock = Instantiate(YellowPrefabs[Random.Range(0, 1)]) as GameObject;
rblock.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
rblock.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
rblock.transform.SetParent(canvas.transform);
grid[i, j] = rblock;
CountYellow++;
}
else {
GameObject block = Instantiate(NonYellowPrefab[Random.Range(0, 2)]) as GameObject;
block.GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);
block.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f) * scale;
block.transform.SetParent(canvas.transform);
grid[i, j] = block;
}
check++;
x += xWidth * space;
}
y -= yHeight * space;
x = xStart;
}
}
}
To start I don't know why would you do that. Technically that is the whole point of having scenes. But still what you could do is put everything (Level 1 and 2) in one scene and then position and disable whatever belongs to Level 2 outside the camera. Then instead of calling Application.LoadLevel I would call a function that will move everything that belongs to Level 1 out of the screen and at the same time moves Level 2 into the screen. If I where you I would group all the objects of one level as child of another GameObject (maybe called LevelX), that way it will be easier to move everything at once. This will even give you nice slide in transition between levels. If you don't want the transition just change the positions instantly.
Be carefull because depending on the amount of levels you could have serious performance issues.
You could use a prefab for the colored blocks and drag it into your different level scenes. If you now attach your behaviour script (managing what happens if a raycast hits it) to your prefab, all instances in all levels will have the same logic. That way you can reuse the same code base for different levels.