- Home /
Switch to objects previous color
Hi, first time posting on the forum, hoping someone can help me with this.
I have a player object who's material color changes to red, green or blue when it comes into contact with a red, green or blue cube. However i want to be able to press the backspace button to switch between the two most recent colors which the player has essentially picked up but have no idea where i would even begin with this.
This is the code I'm using to change the color of the player, hope this all made sense!
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("CCRed"))
{
gameObject.GetComponent<Renderer>().material.color = Color.red;
}
if (other.gameObject.CompareTag ("CCGreen"))
{
gameObject.GetComponent<Renderer>().material.color = Color.green;
}
if (other.gameObject.CompareTag ("CCBlue"))
gameObject.GetComponent<Renderer>().material.color = Color.blue;
}
Answer by Zaeran · Feb 10, 2016 at 01:22 PM
Easiest way to do this would be to add in a 'previousColor' variable, and assign to it when a color swaps.
ie.
private Color prevColor;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("CCRed"))
{
prevColor = gameObject.GetComponent<Renderer>().material.color;
gameObject.GetComponent<Renderer>().material.color = Color.red;
}
if (other.gameObject.CompareTag ("CCGreen"))
{
prevColor = gameObject.GetComponent<Renderer>().material.color;
gameObject.GetComponent<Renderer>().material.color = Color.green;
}
if (other.gameObject.CompareTag ("CCBlue"))
prevColor = gameObject.GetComponent<Renderer>().material.color;
gameObject.GetComponent<Renderer>().material.color = Color.blue;
}
Now you can access that color whenever you need it. You can also create a variable to store the current color, letting you swap between them with whichever key you'd like
Answer by corn · Feb 10, 2016 at 01:24 PM
First you have to store colors as you pick them, and only keep the latest two, since those are the ones you want to be able to switch between. Then you store the material's current color and detect when backspace is hit. When that happens, just assign your second color to the material.
Storing latest two colors
Color[] m_Colors = new Color[2];
int m_CurrentColorIndex = 0;
void SaveColor(Color color)
{
// Remove old color and save new one
m_Colors[0] = m_Colors[1];
m_Colors[1] = color;
gameObject.GetComponent<Renderer>().material.color = color;
m_CurrentColorIndex = 1;
}
Switch current color
void SwitchColor()
{
// If current color index is 0, then new index is 1, and vice versa
int newIndex = (m_CurrentColorIndex == 0) ? 1 : 0;
gameObject.GetComponent<Renderer>().material.color = m_Colors[newIndex];
m_CurrentColorIndex = newIndex;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Backspace))
{
SwitchColor();
}
}
In OnTriggerEnter, call SaveColor instead of directly changing your renderer's color.
Answer by Denvery · Feb 10, 2016 at 01:26 PM
You can use code like this:
private Color _currentColor;
private Color _previousColor;
private void Update()
{
if(Input.GetKeyUp(KeyCode.Backspace))
{
_currentColor = gameObject.GetComponent<Rendered>().material.color;
gameObject.GetComponent<Rendered>().material.color = _previousColor;
_previousColor = _currentColor;
}
}