Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 callanmarch · Feb 10, 2016 at 12:41 PM · gameobject

Switch to objects previous color

Hi, first time posting on the forum, hoping someone can help me with this.

I have a player object who's material color changes to red, green or blue when it comes into contact with a red, green or blue cube. However i want to be able to press the backspace button to switch between the two most recent colors which the player has essentially picked up but have no idea where i would even begin with this.

This is the code I'm using to change the color of the player, hope this all made sense!

 void OnTriggerEnter(Collider other) 
     {    
     if (other.gameObject.CompareTag ("CCRed"))
     {
             gameObject.GetComponent<Renderer>().material.color = Color.red;
 
     }
     if (other.gameObject.CompareTag ("CCGreen"))
     {
             gameObject.GetComponent<Renderer>().material.color = Color.green;
 
     }
     if (other.gameObject.CompareTag ("CCBlue"))
 
         gameObject.GetComponent<Renderer>().material.color = Color.blue;
 
 }

 
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Zaeran · Feb 10, 2016 at 01:22 PM

Easiest way to do this would be to add in a 'previousColor' variable, and assign to it when a color swaps.

ie.

 private Color prevColor;
 
 void OnTriggerEnter(Collider other) 
      {    
      if (other.gameObject.CompareTag ("CCRed"))
      {
              prevColor = gameObject.GetComponent<Renderer>().material.color;
              gameObject.GetComponent<Renderer>().material.color = Color.red;
  
      }
      if (other.gameObject.CompareTag ("CCGreen"))
      {
             prevColor = gameObject.GetComponent<Renderer>().material.color;
              gameObject.GetComponent<Renderer>().material.color = Color.green;
  
      }
      if (other.gameObject.CompareTag ("CCBlue"))
          prevColor = gameObject.GetComponent<Renderer>().material.color;
          gameObject.GetComponent<Renderer>().material.color = Color.blue;
  
  }


Now you can access that color whenever you need it. You can also create a variable to store the current color, letting you swap between them with whichever key you'd like

Comment
Add comment · 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
0

Answer by corn · Feb 10, 2016 at 01:24 PM

First you have to store colors as you pick them, and only keep the latest two, since those are the ones you want to be able to switch between. Then you store the material's current color and detect when backspace is hit. When that happens, just assign your second color to the material.

Storing latest two colors

 Color[] m_Colors = new Color[2];
 int m_CurrentColorIndex = 0;
 
 void SaveColor(Color color)
 {
     // Remove old color and save new one
     m_Colors[0] = m_Colors[1];
     m_Colors[1] = color;
     gameObject.GetComponent<Renderer>().material.color = color;
     m_CurrentColorIndex = 1;
 }

Switch current color

 void SwitchColor()
 {
     // If current color index is 0, then new index is 1, and vice versa
     int newIndex = (m_CurrentColorIndex == 0) ? 1 : 0;
     gameObject.GetComponent<Renderer>().material.color = m_Colors[newIndex];
     m_CurrentColorIndex = newIndex;
 }
 
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.Backspace))
     {
         SwitchColor();
     }
 }

In OnTriggerEnter, call SaveColor instead of directly changing your renderer's color.

Comment
Add comment · 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
0

Answer by Denvery · Feb 10, 2016 at 01:26 PM

You can use code like this:

 private Color _currentColor;
 private Color _previousColor;
 
 private void Update()
 {
          if(Input.GetKeyUp(KeyCode.Backspace))
          {
                 _currentColor = gameObject.GetComponent<Rendered>().material.color; 
                 gameObject.GetComponent<Rendered>().material.color = _previousColor;
                 _previousColor = _currentColor;
          }
 }
Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

MouseClick and gameObject 1 Answer

Add mesh with materials to scene through code. 0 Answers

Objects in Hierarchy Not Visible Anywhere 0 Answers

Do something with GameObject with certain distance 1 Answer


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