Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Dok101 · Mar 18, 2014 at 10:26 PM · materialcolorrenderermainmenu

Changing two different objects renderer colour

Hello I am currently in the process of making a main menu. However one of the problems I'm having is that I cant find a way to change the renderer colour of two objects when the script that changes the renderer colour of the object is only on one of the objects. What I'm actually trying to do is change the colour of the text but also change the colour of the box that is behind it so that when the text is unselected the text is white and the box is black but when the text is selected the text is black and the box is white. I am using the renderer.material.color = Color.white; code method and using a simple OnMouseEnter/Exit method to detect the Mouse hovering over it. Here Is the code of one of the buttons

 #pragma strict
 
 function OnMouseEnter () 
 {
 renderer.material.color = Color.black;
 }
 
 function OnMouseExit () 
 {
 renderer.material.color = Color.white;
 }
 
 function OnMouseDown () {
         Application.LoadLevel ("Initial Scene");
     }

Thanks in Advance for your help.

Comment
Add comment · Show 3
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 rockyourteeth · Mar 19, 2014 at 08:47 PM 0
Share

Can you post your current code? Just makes it easier to understand what you're trying to accomplish...

avatar image Dblfstr · Mar 19, 2014 at 09:07 PM 0
Share

Is the color changing at all? Is this on the text or the button (box). Is the box/button a gameobject? Does it have a material attached that has a color parameter? Is the text a GUItext object attached to the button? These things will also help guide you to a correct solution.

avatar image Dok101 · Mar 19, 2014 at 09:21 PM 0
Share

Yes the color of the text is changing. Its just a 3D text with a box collider but yes it is a gameobject. The text mesh does have a color parameter. The text then has the script which turns it into a button. So what Im trying to do is basically change two gameobjects color to different colors from one script

1 Reply

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

Answer by Gruffy · Mar 20, 2014 at 09:46 PM

Hey Dok101, Here is an old script I made for a project once, please add this to your assets in Unity and in a scene make a TextMesh.

take that textMesh and add it into this script through inspector panel. You may also need to create upto 3 extra materials to show you whats happening during runtime, but i dont think it will throw a wobbly if you dont.

Code....

 using UnityEngine;
 using System.Collections;
 /// <summary>
 /// GUI hover controls.
 /// Forces an audio source to the instance
 /// plays an audio object on mouse up 
 /// changes colour on mouse over and exit
 /// loads or plays based on textmesh being clicked on the screen
 /// 
 /// IMPORTANT!!!
 /// YOU MUST MAKE A TEXT MESH AND THEN APPLY THAT TO THE SLOT IN YOUR SCRIPT FOUND THROUGH INPECTOR.
 /// IT IS ALSO ADIVASBLE TO MAKE THREE MATERIALS(DIFFUSE FINE) AND CHANGE EACH COLOR.
 /// THE FADED OT MATERIAL JUST NEEDS TO BE SEE THOUGH OR HAVE NO TEXTURE OR SOMETHING, SEE UNITY MOBILE SHADERS->TRANSPARENT IN MATERIAL TYPE DROP DOWN
 /// </summary>
 [RequireComponent(typeof(AudioSource))]
 public class GUIHoverControls : MonoBehaviour 
 {
     public int levelToLoad; 
     public AudioClip narrative;
     public AudioClip beep;
     public bool isQuitButton = false;
     public float wait = 5.0f;
     public Material mat1;
     public Material mat2;
     public Material mat3;
     public Material fadedOutMat;
     
     
     public TextMesh txtMesh;
     // Use this for initialization
     private bool isClicked = false;
     /// <summary>
     /// Raises the mouse over event.
     /// </summary>
     void OnMouseOver()
     {
         /// change the material when hovering over
         ChangeColor(mat2);
             
         /// if leftmouse button cllicked
         if(Input.GetMouseButtonUp(0))
         {
             /// change mat to signify click occurence
             ChangeColor(mat3);
             /// if this is a wuit button 
             if(isQuitButton)
             {
              /// audio.clip = clickBeep;
                 audio.PlayOneShot(beep);
                 
                 /// exit game
                 Application.Quit();
             }
             else /// this is a button to load a level
             {
                 
                 Debug.Log ("loading level in 5 secs");
                 /// specifics hack to check if button is start game to allow button audio to yield before starting the level
                 if(txtMesh.text == "Another Button")
                 {
                     /// GUI.Label (new Rect(Screen.width/2,Screen.height /3,100,40), "Click to for Intro");
                     isClicked = true;
                     Debug.Log (isClicked);
                     StartCoroutine(StartIntroTalk());
                 }
                 else if(txtMesh.text == "Play gamE")
                 {
                     isClicked = true;
                     Debug.Log (isClicked);
                     StartCoroutine(LoadALevel());
                 }
                 else /// we checked and it wasnt the start button
                 {    
                     isClicked = false;
                     Debug.Log (isClicked);
                     /// StartCoroutine(LoadChosenLevel());
                 }
                 
             }
         }
     }
     
     /// <summary>
     /// Raises the mouse exit event.
     /// when the mouse exits the textmesh`s collision area
     /// </summary>
     void OnMouseExit()
     {
         /// return colour to normal white faded alpha
         ChangeColor (mat1);    
     }
     
     /// <summary>
     /// Changes the color.
     /// change colour method taking a material in as the argument
     /// </summary>
     /// <param name='mat'>
     /// Mat.
     /// </param>/ <summary>
     /// Changes the color.
     /// </summary>
     /// <param name='mat'>
     /// Mat.
     /// </param>/ <summary>
     /// Changes the color.
     /// </summary>
     /// <param name='mat'>
     /// Mat.
     /// </param>//
     void ChangeColor(Material mat)
     {
         /// if not mat1 or mat 2
         if(mat != fadedOutMat && mat != mat2 && mat != mat3)
         {
             /// mat must be mat 1
             mat = mat1;
         }
         else if(mat != fadedOutMat && mat != mat1 && mat!= mat3)
         {
             /// mat must be mat 2
             mat = mat2;
         }
         else if(mat != fadedOutMat && mat!= mat1 && mat!= mat2)
         {
             /// mat must be mat 3
             mat = mat3;
         }
         else
         {
             /// mat must be mat 4
             mat = fadedOutMat;
         }
         /// assign new material to the textMesh being operated on
         txtMesh.renderer.material = mat;
         
     }    
     /// <summary>
     /// Starts a narrative.
     /// 
     /// </summary>
     /// <returns>
     /// The intro talk after time
     /// </returns>
     IEnumerator StartIntroTalk()
     {
         audio.clip = narrative;
         audio.Play();
         if(isClicked)
         {
             ChangeColor (fadedOutMat);
         }
         yield 
             return
                 new WaitForSeconds(wait);    
     }
     /// <summary>
     /// Loads a level based on a mouse input to start game
     /// </summary>
     /// <returns>
     /// The A level.
     /// </returns>
     IEnumerator LoadALevel()
     {
         audio.PlayOneShot(beep);
         if(isClicked)
         {
             
             ChangeColor (fadedOutMat); /// txtMesh.renderer.material.color.a = new Color(txtMesh.renderer.material.color.r,txtMesh.renderer.color.g,txtMesh.renderer.color.b,0.0f);
         }
         yield 
             return
                 new WaitForSeconds(wait);
                  Application.LoadLevel(levelToLoad);
     }
     
 
 }/// end class
 

Anwyay. Not a complete answer, but a good place to start from. take care and let me know if you have any problems getting script to work (it`ll only be something simple to do with setup i stated above ) Take Care and thanks for reading Gruffy

edit:29.03.2014 Hey bud, here is a package, unzip it, then import it to your project. make a new scene and then drag the "GUIControlObject" prefab to your Main Camera object. The press play and mouse over or click them - also have your console window visible when playing.

Take care bud, and sorry again I did not get back very quick, I honestly didn`t realise you had commented further on your problem. Package here <---Please download this


textmeshmouseovers.zip (68.4 kB)
Comment
Add comment · Show 8 · 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 Dok101 · Mar 21, 2014 at 07:32 AM 0
Share

Thanks Ill get started using that code now

avatar image Gruffy · Mar 22, 2014 at 11:19 AM 0
Share

hey @dok101 how is your solution going ? Are you any further or have you solved it even? Take care bud.

Gruffy

avatar image Dok101 · Mar 22, 2014 at 05:27 PM 0
Share

Thanks Gruffy For some reason when I hover over it the entire text just turns into boxes of whatever material Ive selected in the mat. Thanks for your help

avatar image Gruffy · Mar 29, 2014 at 01:14 PM 0
Share

sorry bud, i didnt realise you replied to this. whats the problem sorry > ?

avatar image Gruffy · Mar 29, 2014 at 01:17 PM 0
Share

by your above comment, im guessing you have not reduced the code to fit yours etc and are experiencing issue with swaps because you have no materials specified in the inspector etc.

to resolve your above issue, make three other materials and add them into your script at the inspector panel, then press play. alternatively, i will be back in 15 $$anonymous$$s, with a package for you to load into unity and press play to see it demoing. take care brb Gruffy

Show more comments

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

23 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

Adjust tint color of particles/additive? 2 Answers

Reduce Skid Mark Alpha Gradually 0 Answers

How would I change the color of a texture within an object with multiple textures? 1 Answer

need help resolving 'renderer.material.color' conflict. 3 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