0
votes

I want to create a game where all tiles must be rotated to the correct position. I have 15 tiles with a script attached which rotates them 90 degrees on each click. I can detect when each tile is in the correct position but what I am having trouble doing is detecting when ALL 15 are in the correct position.

Here is a screen shot of my stage

https://www.dropbox.com/s/9ag4ly2bil1mzf1/Untitled-1.jpg?dl=0

I was thinking I can create an empty game object and attach a new script with code to get the value from each tile and check if all are true (in the correct position).

Here is my basic code for the rotation script

using UnityEngine;
using System.Collections;

public class rotate : MonoBehaviour {

    public float rotationAnswer;
    // Use this for initialization
    void Start() {}

    // Update is called once per frame
    void Update() {}

    float rotationValue = 0;

    void OnMouseDown(){
        transform.Rotate(Vector3.up * 90);
        rotationValue = transform.rotation.eulerAngles.y;
        Debug.Log ( rotationValue );

        if ( rotationValue == rotationAnswer)
        {
            Debug.Log ( "Correct" );
            switch (this.name )
            {
            case "tile1":

            }

            Debug.Log ( GameObject.Find("GameComplete").GetComponent<TileGameComplete>().totalCorrectTiles ))

        } else {
        Debug.Log ( "Wrong" );
        }
    }
}

Here is my basic code for the detection and complete game script

using UnityEngine;
using System.Collections;

public class TileGameComplete : MonoBehaviour {

    public int currentCorrectTiles = 0;
    public bool tile1 = false;
    public bool tile2 = false;
    public int totalCorrectTiles = 15;
    // Use this for initialization

    void Update() 
    {
        GameObject.Find("Tile1").GetComponent<rotate>().done
        if (tile1 && tile2) 
        {
            Debug.Log ("You've won");
        }
    }
}

How would you go about solving this problem in C#. Thanks :)

3
Ahhh...pick one bracket style and stick to it! - JNYRanger
@JNYRanger: to be fair, a lot of unity devs are extremely new to programming. A lot of this code is likely copy/paste. - NotMe
@NotMe I know, I know. But it's still frazzling to my brain. Also Allman Style for life. - JNYRanger

3 Answers

2
votes

Keep a shared or static variable that gets set to zero at the start of the level. When a shape is moved into the correct rotation add one to the count, and if a shape is moved out of correct rotation subtract one from the count. When the count is equal to 15 the puzzle is solved.

0
votes

I'm at work and can't access Unity right now so can't give you code, but if you make all those 15 tiles children of another (empty) GameObject in your scene, you can then access that parent object's children collection and iterate through that to check if all of them are correctly rotated.

0
votes

@Agumander has a good answer, but personally I prefer to keep logic simple. I would recommend creating a GameController that has access to all 15 tiles, either via a public static list or GameObject.find(...) That way you won't have to check the list 15 times each time the game update for each tile. The tile just has to do their own job (Rotate) And the game controller will determine the logic to end/restart the game