Referencing color change when object is 'deselected'

Hello, it’s been some time since I last had to use unity answers to find a solution to a problem but I found it very useful last time I visited.

Essentially, I have a raycast script attached to my main camera (named ‘select’) which is telling the following 'interact’script whether or not an object is selected (highlighting it red) so the player can interact with said item. At the moment, the script works very well with objects that are specifically static in colour, as I can simply get the script to return to a white state or any other colour when the object is not selected or the selection is false, that C# script states the following.

using UnityEngine;
using System.Collections;

public class Interact : MonoBehaviour {
public GUIText target;
public AudioClip interaction;
private bool selected = false;
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
		renderer.material.color = Color.white;
		target.text = "";
		selected = false;
	
	}
	
	public void OnLookEnter(){
	renderer.material.color = Color.red;	
	target.text = "Press P to Play";
	selected = true;
	}
	
	void OnGUI(){
		Event p = Event.current;
		if(p.isKey && p.character == 'p' && selected){
		AudioSource.PlayClipAtPoint(interaction, new Vector3(5, 1, 2));

		}

	}
}

Now here comes the complicated part. Attached to the same object is a script which changes the colour of the object upon a button press.

using UnityEngine;
using System.Collections;

public class color : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {

            if(Input.GetKeyDown(KeyCode.V))
		{
			gameObject.renderer.material.color = Color.white;
            }
		if(Input.GetKeyDown(KeyCode.B))
		{
			gameObject.renderer.material.color = Color.blue;
		}
		if(Input.GetKeyDown(KeyCode.N))
		{
			gameObject.renderer.material.color = Color.green;
		}
		if(Input.GetKeyDown(KeyCode.M))
		{
			gameObject.renderer.material.color = Color.red;
		}
	}
}

Both scripts work, however instead of returning to:

         renderer.material.color = Color.white;

I want the object/script to reference the colour which is currently selected via the ‘color’ selection script instead of returning to white when the selection of the object is false. Any help in the matter will be appreciated

Thank you.

That’s relatively easy. In your “color” script introduce a variable

public Color lastColor = Color.white;

Then when you select a color with the keys assign the selected color like so:

if(Input.GetKeyDown(KeyCode.B))
{
     gameObject.renderer.material.color = Color.blue;
     lastColor = Color.blue;
}

Inside the other script “Interact” you get a reference to your other script and assign the selected color variable like so:

 void Update () {
 
     renderer.material.color = gameObject.GetComponent<color>().lastColor;
     target.text = "";
     selected = false;
 
 }

Strongly is suggested to cache also the “color” component when you use update.

EDIT: I can see a better solution here though. Instead of constantly updating and checking you can do the following: In your “Interact Script” add a method:

public void SetGameObjectColor(Color color)
{
  renderer.material.color = color;
}

And inside the “script” when you perform a key down just call the method like so:

if(Input.GetKeyDown(KeyCode.B))
{
   gameObject.GetComponent<Interact>().SetGameObjectColor(Color.blue);
}

It is a far much better approach.

I am not sure this is what you asked but i tried your script if you just want change color when V,B,N,M key pressed then remove “}” in the Line No 30 and put that in Line No 17… sorry if it not help you .