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.