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 bdiddy12 · Apr 27, 2012 at 04:11 PM · prefabmaterialonmouseexit

Assigning materials based on a Prefab

Hello!

I'm looking for some help with a very confusing problem. I have a top down game with an inventory and collection system. When a player hovers over an item, it turns either green or red based on whether it is within range to pick up or not. This works fine, but when I want to return the object to it's original colors, I run into some problems. Here is my code:

 function OnMouseExit ()
 {
     var renderers = GetComponentsInChildren(Renderer);//get all the renderers in the object
     var children = thisPrefab.gameObject.GetComponentsInChildren.<Renderer>();//get all the original renderers
     var child : int = 0;
     for(var rend : Renderer in renderers)
     {
         for(var i=0; i < rend.materials.length; i++)
         {
             rend.materials[i].color = children[child].renderer.materials[i].color;
             print("child = " + child + " i= " + i);
         }
         child++;
     }
 }

My print function, just for debugging, returns exactly what I would expect. For each object it returns exactly the number of materials on that object, but nothing changes at all. When the scene loads, the object is it's own color, when I mouse over it changes to red/green, but when this fires nothing at all happens. I get no error message.

Anyone have any ideas that can point me in the right direction?

Thanks a lot!

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 bdiddy12 · Apr 27, 2012 at 05:40 PM

I got it to work using something similar to what Thomas suggested. I feel it is a bit sloppy, but it gets the job done and only needs to be applied to the parent object.

 var savedColors : Color[,] = new Color[10,10];
 function Start()
 {
     var renderers = this.gameObject.GetComponentsInChildren.<Renderer>();
     for(var j=0; j < renderers.length; j++)
     {
         for(var i=0; i < renderers[j].materials.Length; i++)
         {
             savedColors[j,i] = renderers[j].materials[i].color;
         }
     }
 }
 function OnMouseExit ()
 {
     var renderers = this.gameObject.GetComponentsInChildren.<Renderer>();
     for(var j=0; j < renderers.length; j++)
     {
         for(var i=0; i < renderers[j].materials.Length; i++)
         {
             renderers[j].materials[i].color = savedColors[j,i];
         }
     }
 }
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 tingham · Apr 27, 2012 at 04:40 PM

What I'm doing for this is that I have a really simple behavior attached to anything inside of my prefab that includes a MeshRenderer - this component has a public List that matches per-index the materials that are set when the object Starts.

This allows me to revert every material (in position in the materials array) back to it's original shader. This works but performance is not great (draw calls shoot up.)

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 tingham · Apr 27, 2012 at 04:40 PM 0
Share

What I've been thinking about doing is implementing a RenderTexture from a camera matching the current camera, that renders only the item I'm interested in (as a clone) with the new materials applied; or "possibly" using the alpha from Camera A to modulate what the user sees under the cursor. Haven't really sorted all of that out yet.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can I set the material on an instance after creating it? 1 Answer

Prefab loses its materials after running the game - Why? 1 Answer

How to remove not using dependencies? (in matetial, prefab) 1 Answer

Apply new complex fbx to saved prefab 0 Answers

When setting a local scale to an instantiated gameObject, it loses material. 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