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 HalversonS · Apr 10, 2014 at 06:39 PM · color changematerial colorhighlightgetcomponentinchildren

Excluding certain children from a loop (changing parented children's colors on mouseover)

My script is looking at each child component in a parent and saving the original color during Start. It is then, during mouseover, multiplying the color value of each component's material (this is to give it a 'highlight' appearance).

Then, OnMouseExit(), it is supposed to return the color to the original. However all children components are returning blue. There is a child in the parent that doesn't use a .png and instead is using a material with the Main Color set to a blueish hue.

I was able to get them to return to the right color... however the water then takes uses their color.

There are many gameobjects like this and I'd rather not tweak each one or create a unique script for each one...

Here is my current code:

 #pragma strict
 
  
  var highlightValue = 1.50;
  var originalColor = Color();
  var children : Renderer[];
  
 function Start()
 {
    children = GetComponentsInChildren.<Renderer>();
    
    for(var i : Renderer in children)
    {
     originalColor = i.material.color;
    }
 }
  
 function OnMouseOver()
 {
 
  for(var i : Renderer in children)
    {
      var wasLit : boolean = false;
      
         if (wasLit == false)
       {
        i.material.color = originalColor*highlightValue;
        wasLit = true;
       }
    }
 }
 
 function OnMouseExit()
 {
  for(var i : Renderer in children)
   { 
     i.material.color = originalColor;   
   }
 }
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 HalversonS · Apr 10, 2014 at 07:28 PM

Okay, so this works :P I didn't realize that I could check to see if there was a texture attached to a component. http://docs.unity3d.com/Documentation/ScriptReference/Material-mainTexture.html

Added a couple if statements. Every child in a parent highlights now if it has a texture.

 #pragma strict
 
  
  var highlightValue = 1.50;
  var originalColor = Color();
  var children : Renderer[];
  
 function Start()
 {
    children = GetComponentsInChildren.<Renderer>();
    
    for(var i : Renderer in children)
    {
    if(i.material.mainTexture != null)
     {
     originalColor = i.material.color;
     }
    }
 }
  
 function OnMouseOver()
 {
 
  for(var i : Renderer in children)
    {
      var wasLit : boolean = false;
      
         if (wasLit == false && i.material.mainTexture != null)
       {
        i.material.color = originalColor*highlightValue;
        wasLit = true;
       }
    }
 }
 
 function OnMouseExit()
 {
  for(var i : Renderer in children)
   {
   if(i.material.mainTexture != null)
    {
     i.material.color = originalColor;
    }
   }
 }
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
1

Answer by Mikael-Gyth · Apr 10, 2014 at 07:06 PM

In your start method you iterate over all objects in children and set originalColor to the color of the object. originalColor is going to end up beeing the color of the last item in the collection every time.

in OnMouseExit you set all the objects colors to originalColor (the same color as the last object in the collection).

If the objects have different colors you will need to store the original colors in a collections too. That way you can set the "original" originalColor in OnMouseExit.

 var originalColor : Color[]

and

 var index : int = 0;
 for(var i : Renderer in children)
   { 
     i.material.color = originalColor[index];
 index++;
   }

For exclusion of objects I'd go with tags and only store the color of objects with a certian tag.

(PS. I'm not too good with UnityScript so the code might not be 100%)

Comment
Add comment · Show 3 · 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 HalversonS · Apr 10, 2014 at 07:19 PM 0
Share

Right, but the vast amount of items I have vary in such that I'd have to individually change each one. This could take days :P if not weeks. (I'm talking a ridiculous amount of objects)

I'm trying to get whatever script I'm running to be applied to any object that needs a mouseover highlight and have it function the same without adjusting any additional assets or components. It works great on gameobjects without children and gameobjects that have a texture on each renderer.

avatar image HalversonS · Apr 10, 2014 at 07:22 PM 0
Share

I just gave myself an idea with the way I phrased the last sentence ^ waiting for everything to build.

avatar image HalversonS · Apr 10, 2014 at 07:31 PM 0
Share

I'll keep what you posted in $$anonymous$$d for my next project. That way I can build from the ground up without having to go back and change a million things. Thanks $$anonymous$$ikael!

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

22 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

Related Questions

Material.SetColor and Material.color are not working properly 0 Answers

What is the best way to have different colored material on a batch of the same 3D object? 1 Answer

Specific colors change gradually as I keep playing on Android! 0 Answers

How to gradually transition between sprites? 1 Answer

Car Model Run time Color Change Problem 0 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