Skip to content

Instantly share code, notes, and snippets.

@TheCuttlefish
Created November 11, 2020 17:09
Show Gist options
  • Save TheCuttlefish/366256837bd6d59adfce6da9fa7d8a19 to your computer and use it in GitHub Desktop.
Save TheCuttlefish/366256837bd6d59adfce6da9fa7d8a19 to your computer and use it in GitHub Desktop.
On colour click move objects
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ButtonScript : MonoBehaviour
{
public int myColour;
public GameObject player;
private void Start()
{
if (myColour == 0) GetComponent<SpriteRenderer>().color = Color.red;
if (myColour == 1) GetComponent<SpriteRenderer>().color = Color.green;
if (myColour == 2) GetComponent<SpriteRenderer>().color = Color.blue;
if (myColour == 3) GetComponent<SpriteRenderer>().color = Color.yellow;
player = GameObject.Find("player");
}
private void OnMouseDown()
{
player.GetComponent<Player>().RecieveColour(GetComponent<SpriteRenderer>().color);
}
}
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