Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Pattington_Bear · Dec 09, 2014 at 08:07 AM · c#raycastobjectcolorchange

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Pattington_Bear · Dec 09, 2014 at 09:02 AM 0
Share

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.

avatar image Pattington_Bear · Dec 09, 2014 at 09:38 AM 0
Share

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.

avatar image
0

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 .

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Pattington_Bear · Dec 09, 2014 at 09:05 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges