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 General-Troll · Dec 29, 2015 at 04:16 AM · inputmaterialcolorrenderer

Material Color Won't Change On Input

Ok, so I have been working on a simple mobile game. Right now I am making the script around PC (for ease of testing). The problem is that when ever I try to change the cube's color, it will change once, but from then on the material color won't change. I have experimented with different colors and different materials, but the cube's material color never changes. Here is my script (NOTE: The script isn't just for the material's color change, but also for some physics, as I will need to incorporate both together later, if this changes anything):

 #pragma strict
 var jumpForce = 10;
 var jumping = false;
 var isGrounded = true;
 var jumpValue = 0;
 
 function Start() {
 renderer.material.color = Color.blue;
 }
 
 function Update () {
     if(Input.GetKeyDown(KeyCode.Space) & isGrounded) {
      isGrounded = false;
      rigidbody2D.velocity = new Vector2(0, jumpForce);
     }
     if(Input.GetKeyDown(KeyCode.Q) && renderer.material.color == Color.red) {
      renderer.material.color = Color.blue;
      print ("called");
     }
     if(Input.GetKeyDown(KeyCode.Q) && renderer.material.color == Color.blue) {
      renderer.material.color = Color.red;
     }
     transform.rotation = Quaternion.identity;
 }
 
 function OnCollisionEnter2D (other : Collision2D) {
 isGrounded = true;
 }

 
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

1 Reply

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

Answer by maccabbe · Dec 29, 2015 at 05:06 AM

The issue is that the color switches from red to blue and immediately back to red.

If the color is red then

  if(Input.GetKeyDown(KeyCode.Q) && renderer.material.color == Color.red) {
     renderer.material.color = Color.blue;
     print ("called");
  }

Gets called and the color is set to blue.

The color is now blue so when the next statement is run

  if(Input.GetKeyDown(KeyCode.Q) && renderer.material.color == Color.blue) {
      renderer.material.color = Color.red;
  }

the color is immediately set back to red.

There should be an if/else if/else, i.e.

  if(Input.GetKeyDown(KeyCode.Q) && renderer.material.color == Color.red) {
     renderer.material.color = Color.blue;
     print ("called");
  }
  else if(Input.GetKeyDown(KeyCode.Q) && renderer.material.color == Color.blue) {
      renderer.material.color = Color.red;
  }
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 General-Troll · Dec 30, 2015 at 02:28 AM 0
Share

Thanks a lot! Turns out that when I called the function, they were being called on the same frame due to it switching (the color was red, but since it is now blue, it goes back to red because it all occurred at the same frame). Never thought of using an else if statement though. Thanks a bunch anyways!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Changing two different objects renderer colour 1 Answer

Material doesn't have a color property '_Color' 4 Answers

Massive FPS hit when changing colors on the fly 3 Answers

When changing the color of an instantiated object, I keep getting an error - how do I fix this? 0 Answers

render.material.color is not working, or at least not completely? 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