I put a code on a sprite and I made it so that when you click on it it will change color.
void OnMouseDown() { state = 2; GetComponent<SpriteRenderer> ().color = Color.green; }
I want it to change back when clicking on something else.
How do I make something happen when clicking on something else?
You have to save the original color to some variable and then onMouseDown() after you change the color of the current sprite, assign the saved color to the previous sprite using the SpriteRenederer. I made this click test earlier for a client with a similar functionality. I cannot share the code here due the confidentiality thing with client but I hope you can figuire out with approach I just shared.
1: https://www.clickspeedtester.com/,You have to save the original color to some variable and then onMouseDown() after you change the color of the current sprite, assign the saved color to the previous sprite using the SpriteRenederer. I made this click test earlier for a client with a similar functionality. I cannot share the code here due the confidentiality thing with client but I hope you can figuire out with approach I just shared.
If relevant, then you can use a list with all objects that can be selected:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class YourScript : MonoBehaviour
{
static public List<YourScript> objectsOfYourScript = new List<YourScript>();
int state = 1;
Color notSelectedColor, selectedColor = Color.green;
[HideInInspector] public SpriteRenderer mySpriteRenderer;
void Start()
{
objectsOfYourScript.Add(this);
mySpriteRenderer = GetComponent<SpriteRenderer>();
notSelectedColor = mySpriteRenderer.color;
}
void OnMouseDown()
{
for (int i = 0; i < objectsOfYourScript.Count; i++)
if (objectsOfYourScript *== this)*
{
mySpriteRenderer.color = selectedColor;
}
else
{
objectsOfYourScript*.mySpriteRenderer.color = notSelectedColor;*
}
}
}