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 csciguy · Nov 22, 2010 at 10:54 PM · texturemeshmaterialrendererskinnedmeshrenderer

Swapping out materials on a skinned mesh renderer?

All,

I have a single gameobject with a Skinned Mesh Renderer. This renderer has a single material.

I want to swap out this material in game, depending on the situation.

I currently have another material that I would like to swap this out with.

How would I do this via script?

I've tried something like this:

gameObject.renderer.material.mainTexture = (Texture)Resources.Load("black");

But this did not work. Ideas?

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

3 Replies

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

Answer by skovacs1 · Nov 23, 2010 at 12:04 AM

You talk of swapping a material, but then your code tries to swap a texture?

Unity does not build into its target builds assets that are not in use. Using Resources.Load will attempt to load the asset at the path specified relative to a Resources folder.

Do you have a Resources folder? Does this folder have a Texture asset called black?

You can use the resources folder if you like, but it will be slower and depends on all of the assets being deployed in that folder and all of your loading must be path specified.

A faster alternative is to store a reference to the material/texture you want to swap like so:

var swapMat : Material; private var originalMat : Material;

function Start() { if(renderer && renderer.material) originalMat = renderer.material; }

//Somewhere else if(swapMat) renderer.material = swapMaterial;

For Texture2D's, it would be about the same.

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 csciguy · Nov 23, 2010 at 01:34 AM 0
Share

I completely keep forgetting that I can expose a public variable in the editor and drag/drop a material/whatever to that variable. I was able to get this working just fine with a single line of code after that.

Thanks!

avatar image
0

Answer by sirdavid23 · Jun 19, 2012 at 08:26 PM

Could you post the code that worked for you, please? I have the same problem.Thanks.

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
0

Answer by Sinox111 · Mar 09, 2015 at 06:20 PM

gameobject.renderer.material.mainTexture = Resources.Load ("black", typeof(Texture2D))as Texture2D;

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

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

2 People are following this question.

avatar image avatar image

Related Questions

How to switch the texture being used by the material of the mesh renderer 1 Answer

Changing two different objects renderer colour 1 Answer

Why are my textures on planes flipped / reversed left-to-right? 1 Answer

Modifying material modifies the actual .mat file! 0 Answers

How can i change detail texture? 1 Answer


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