Created
November 11, 2020 17:09
-
-
Save TheCuttlefish/366256837bd6d59adfce6da9fa7d8a19 to your computer and use it in GitHub Desktop.
On colour click move objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class Player : MonoBehaviour | |
{ | |
public List<Color> colours = new List<Color>(); | |
public List<GameObject> directions = new List<GameObject>(); | |
public void Start() | |
{ | |
Shuffle(); | |
} | |
void Shuffle() | |
{ | |
colours.Add(Color.red); | |
colours.Add(Color.green); | |
colours.Add(Color.blue); | |
colours.Add(Color.yellow); | |
for (int i = 0; i < directions.Count; i++) | |
{ | |
int rndColour = Random.Range(0, (colours.Count)); | |
directions[i].GetComponent<SpriteRenderer>().color = colours[rndColour]; | |
colours.RemoveAt(rndColour); | |
} | |
} | |
public void RecieveColour(Color buttonColour) | |
{ | |
foreach(var d in directions) | |
{ | |
if(buttonColour == d.GetComponent<SpriteRenderer>().color) | |
{ | |
if (d.name == "up") transform.Translate(0, 1, 0); | |
if (d.name == "down") transform.Translate(0, -1, 0); | |
if (d.name == "left") transform.Translate( -1, 0, 0); | |
if (d.name == "right") transform.Translate( 1,0, 0); | |
} | |
} | |
Shuffle(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment