I have four gameobjects into my scene and i want to change the sprite renderer’s color of each game object based on an input. Ex: if “q” is down change the first game object color to green then check if the key isnt pressed anymore so it goes back to normal. Do that for the 3 other game objects but with different inputs. “w” for the 2nd one, “o” for the 3rd one and “p” for the 4th one. I already made the code to detect the input and change the color. But it does it for all of the gameobjects and not a specific one. How can i change a specific gameObject from my scene without having to make a different script for all of the gameObjects?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
SpriteRenderer sr;
Color sr_NewColor;
float sr_Green, sr_White;
// Start is called before the first frame update
void Start()
{
sr = GetComponent<SpriteRenderer>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("q"))
{
sr.color = Color.green;
}
if (Input.GetKeyUp("q"))
{
sr.color = Color.white;
}
}
}