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
6
Question by Tides · Oct 11, 2011 at 06:27 AM · materialrendererskinned

renderer.materials not working.

Hey guys, here is my code.

 function Start() {
 
     selection = Random.Range(0, allBTextures.Length);
     
     skeleton = this.transform.FindChild("skeleton").transform.GetComponent(SkinnedMeshRenderer);
     bodyMat = allBTextures[selection];
     headMat = allHTextures[selection];
     skeleton.materials[0] = bodyMat;
     skeleton.materials[1] = headMat;
 
 }

The problem is that the materials don't change.

If I change it to 'skeleton.material = bodyMat' then it works but of course only changes the first material, leaving the second on the same.

With both set to 'skeleton.materials' it doesn't throw any errors rather it just doesn't change the material. Why is this? As far as I knew this was meant to worked.

Cheers.

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

Answer by Tct1856 · Oct 11, 2011 at 07:35 AM

Try like this:

 Material[] mats = new Materials[]{bodyMat, headMat};
 skeleton.materials = mats;

This is the same with all Unity objects when you try to access them with [].

Comment
Add comment · Show 5 · 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 AlexTuduran · Jan 14, 2017 at 11:00 AM 0
Share

It should be a way to assign individual elements and let Unity know the array changed, so that it re-evaluates its content. It's a bit silly both from a conceptual and a performance point of view that you need to re-assign a whole array if you need to change just a single element. Good workaround though.

avatar image AlexTuduran · Jan 14, 2017 at 11:11 AM 2
Share

Apparently it can be done without using new, which should yield better performance and also allows you to change just what you need from the array:

 $$anonymous$$aterial[] mats = skeleton.materials;
 mats[1] = head$$anonymous$$at;
 skeleton.materials = mats;

You need a temporary var though. It doesn't work if you change directly in skeleton.materials[1] and just assign skeleton.materials to itself.

avatar image Stankiem AlexTuduran · May 12, 2017 at 11:55 PM 0
Share

Thanks $$anonymous$$Tuduran and Tct1856 This thread was a big timesaver for me! Couldn't figure out why in the heck I couldn't set my materials directly..

avatar image Bunny83 AlexTuduran · May 13, 2017 at 12:16 AM 0
Share

Since the question was already bumped i'd like to add that using those 3 lines are equally bad performance wise. When you "read" the materials property, Unity will create a new temporary array and fill it with the current $$anonymous$$aterial instances of the renderer. $$anonymous$$eep in $$anonymous$$d that Unity is mainly written in C++ and not in C#. When you pass a managed array with the materials to the materials property, the setter of that property will actually copy the references into a native code structure inside the engine's core. When you read the property the getter is invoked which does the reverse. So it creates a new temporary managed array and fills it with the material references stored on the native side.

If you need to change the materials frequently you should simply store the managed array in a class variable and just reuse that array. That way there's no need to create a new array each time you want to replace a $$anonymous$$aterial.

Unfortunately Unity uses a similar concept in multiple other situations as well. Especially for beginners it would be better to not have properties but rather concrete Get and Set methods. Concrete methods make it more clear what happens. For example:

 $$anonymous$$aterial[] mats = skeleton.Get$$anonymous$$aterials();
 mats[1] = head$$anonymous$$at;
 skeleton.Set$$anonymous$$aterials(mats);

In this case it would be quite obvious that this probably doesn't work:

 skeleton.Get$$anonymous$$aterials()[1] = head$$anonymous$$at;

because you can see the clear seperation of getting the array and setting the array. A property combines those two methods into one thing but they are in fact still two seperate methods.

avatar image AlexTuduran Bunny83 · May 13, 2017 at 02:11 PM 0
Share

It makes sense, you're right.

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

6 People are following this question.

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

Related Questions

Changing two different objects renderer colour 1 Answer

Swapping out a single material on a SkinnedMeshRenderer at runtime 0 Answers

Massive FPS hit when changing colors on the fly 3 Answers

Setting Textures causes GameObject Material to turn black? 1 Answer

Webcamtexture not showing on a SpriteRenderer 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