Skip to content

Instantly share code, notes, and snippets.

@TheCuttlefish
Created December 14, 2020 16:57
Show Gist options
  • Save TheCuttlefish/137ca5a1a77431eadcd0658cb6d0f195 to your computer and use it in GitHub Desktop.
Save TheCuttlefish/137ca5a1a77431eadcd0658cb6d0f195 to your computer and use it in GitHub Desktop.
Setting random Colour + Destroying objects
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RandomColour : MonoBehaviour
{
public int randomValue;
public float randomTimerValue;
public float randomDestoyValue;
public float randomXpos;
public float randomYpos;
public Color colour1, colour2, colour3;
float timer;
float maxTimer = 0.1f;//half a second
private void Start()
{
randomXpos = Random.Range(-5f, 5f);
randomYpos = Random.Range(-5f, 5f);
transform.position = new Vector3(randomXpos, randomYpos, 0);
randomTimerValue = Random.Range(0.1f, 1f);
randomDestoyValue = Random.Range(1f, 5f);
Destroy(gameObject, randomDestoyValue);
}
void Update()
{
ChangeColourOnTime(randomTimerValue);
}
public void ChangeColourOnTime(float _seconds)
{
timer += Time.deltaTime;
if (timer > _seconds)
{
randomValue = Random.Range(0, 3);
if (randomValue == 0)
{
GetComponent<MeshRenderer>().material.color = colour1;
}
if (randomValue == 1)
{
GetComponent<MeshRenderer>().material.color = colour2;
}
if (randomValue == 2)
{
GetComponent<MeshRenderer>().material.color = colour3;
}
timer = 0f;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment