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 GregoryNeal · Nov 22, 2013 at 03:41 AM · materialcolorapplication.loadlevelrenderer.material.color

GameObjects color changes on initial load, just not afterwards.

Hello, I've been trying to figure this out on my own through script reference, google, etc, but I can't seem to do it, so I've turned to the community. I've got a main menu for my game, it's pretty simple, just 4 cubes with a text engraving in each that highlight their functions (play, settings, about, quit). When the user mouses over them, they are supposed to move back a bit along the z-axis and turn red, signaling that it is highlighted. So far so good. When the user hits play, the game loads the first level, then if they decide to head back to the main menu, they just pause and select main menu. But now when Unity loads the main menu, the blocks don't change color! They still move and all, but they stay dull and gray. Here is my code for the menu blocks (they all run the same script):

 using UnityEngine;
 using System.Collections;
 
 public class MenuButtonScript : MonoBehaviour {
     
     public Color initColor = new Color(0,0,0,0);
     public Vector3 initPos = new Vector3(0,0,0);
 
     void Start () {
         initColor = gameObject.renderer.material.color;
         initPos = gameObject.transform.position;
     }
     
     void Update () {        
     }
     
     void OnMouseOver(){
         renderer.material.color -= new Color(0,2f,2f) * Time.deltaTime * 2f;
                 if(Input.GetMouseButtonDown(0)){
             if(gameObject.name == "PlayButton"){
                 Debug.Log ("play");
                 Application.LoadLevel(1);
             }
             else if(gameObject.name == "SettingsButton"){
                 Debug.Log("settings");
             }
             else if(gameObject.name == "AboutButton"){
                 Debug.Log ("about");
             }
             else if(gameObject.name == "QuitButton"){
                 Application.Quit();
             }
             else{}
         }
         
     }
     
     void OnMouseEnter(){
         transform.position = new Vector3(initPos.x,initPos.y,initPos.z + 10f);
         
 
     }
     
     void OnMouseExit(){
         renderer.material.color = initColor;
         transform.position = initPos;
     }
 }

BTW, you don't need to explain like I'm a 5 year old, however, I am new to unity. Thanks for any help!

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
0
Best Answer

Answer by iwaldrop · Nov 22, 2013 at 05:07 AM

The OnMouseOver method will be called every frame that your mouse is over the object. What you should really use is the OnMouseDown method, and something like this:

 using UnityEngine;
 using System.Collections;
 using System;
 
 public class ColorChangeOnMouseOver : MonoBehaviour
 {
     public float transitionTime;
     public Color defaultColor;
     public Color hoverColor;
     public Color pressedColor;
 
     private bool isHovering;
 
     void Awake()
     {
         renderer.material.color = defaultColor;
     }
 
     void OnMouseEnter()
     {
         isHovering = true;
         SetColor(hoverColor);
     }
 
     void OnMouseExit()
     {
         isHovering = false;
         SetColor(defaultColor);
     }
 
     void OnMouseDown()
     {
         SetColor(pressedColor, ()=>
         {
             // do whatever the button is supposed to do here; load level, etc.
             if (isHovering)
                 SetColor(hoverColor);
         });
     }
 
     void SetColor(Color targetColor, Action callback = null)
     {
         StartCoroutine(SetColorCoroutine(targetColor, callback));
     }
 
     IEnumerator SetColorCoroutine(Color targetColor, Action callback)
     {
         float startTime = Time.time;
         float endTime = startTime + transitionTime;
         Color startingColor = renderer.material.color;
         while (endTime > Time.time)
         {
             renderer.material.color = Color.Lerp(startingColor, targetColor, (Time.time - startTime)/transitionTime);
             yield return null;
         }
 
         if (callback != null)
             callback();
     }
 }
 


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 GregoryNeal · Nov 22, 2013 at 05:22 AM 0
Share

Okay, thanks. Your code looks good to me, however, I'm not familiar with coroutines, from looking at your code I assume that it's what drives the color change though.

avatar image iwaldrop · Nov 22, 2013 at 06:51 AM 0
Share

Think of coroutines as update loops where you control their starting and stopping individually, and where you can have several running concurrently. The yield statement tells Unity to come back to that point of the method again on the next frame. The fact that it's in a while loop makes it repeat until a condition is met; in this case it's based on a timer.

Coroutines can also be used for asynchronous actions that you would like to perform. Also in this case, I used an Action object as a callback to let me know when the method has finished running. It's what will allow the button color to change to the highlighted color and then run the method.

Checkout the unity docs, and/or google coroutines and the C# Action object to learn more. Also, let me know if your have any more questions about this code, or if it's not working for you. Otherwise, please accept this as answered.

Thanks.

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

Changing two different objects renderer colour 1 Answer

Is it possible to change an entity's RenderMesh color in ECS just like the renderer.material.color in monobehavior? 1 Answer

Accessing material.color removes the material... 1 Answer

How can I change the color of an object's material, not generate another instance of the material, and still keep batching? 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