- Home /
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.
Answer by sysameca · Dec 09, 2014 at 08:25 AM
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.
Works well, throwing some odd little errors but I'll be sure to iron those out. Thanks for the help!
Edit: Giving the second approach a go.
The second approach seems to be less intensive but doesn't seem to achieve what I want it to, only changing the colour of certain areas highlighted by the interact script (including other objects with the script), after maybe changing colour more than once.
Answer by MrTiger · Dec 09, 2014 at 08:34 AM
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 .
Thanks for the response and thanks for noticing the error in my post because I added that as a new line whilst I was typing out the question. Both scripts do work but my point was to try to get the interact script to reference the colour changes, something solved by sysameca above.
Your answer
Follow this Question
Related Questions
How do i make the object at the end of my raycast not phase into a wall? 2 Answers
Changing a second objects material on trigger 0 Answers
Distribute terrain in zones 3 Answers
change color object 2 Answers
Multiple Button Modifications 2 Answers