• 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
Question by shakh · Nov 05, 2015 at 09:10 PM · playmodechange material

How to replace material in playmode?

Hello,

Lets say i have a chair in scene. I would like to change its material/shader/texture when in play mode. Maybe to be able to choose from a set of materials. Maybe to also be able to add a video as a material and be able to change it in the same way(as television or projection). Do you know if there is by any chance script that would help? I do not program myself. There is no problem buying the script, I just have not been lucky to find one.

Thank you!
Matej

Comment

People who like this

0 Show 6
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 DeathDev · Nov 05, 2015 at 11:24 PM 0
Share

Do you need a script for changing an object's material from a pre-set material to another pre-set material, or do you need to create a new material altogether?

avatar image shakh · Nov 06, 2015 at 09:26 AM 0
Share

Just change pre-set material, and to be able to pick from a range of different ones. Thank you!

avatar image MakakWasTaken · Nov 06, 2015 at 10:05 AM 0
Share

If you want to play a movie, try taking a look at this: https://www.youtube.com/watch?v=fQH2hH9Abdo

avatar image ben-rasooli · Nov 06, 2015 at 11:57 AM 0
Share

What do you mean by "in playmode"? Do you mean change the material at runtime via script? Or do you mean changing a material in playmode and keep the change after stopping the playmode?

avatar image shakh · Nov 07, 2015 at 09:03 PM 0
Share

Thanks for the film!

I mean something like this here: https://www.youtube.com/watch?v=WyAg0h7uxv8 at 1.15min

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by screenname_taken · Nov 06, 2015 at 12:15 PM

You just need a script. If they are complex shaders, then you'd need to have the materials prepared before hand in the editor. (It's just easier that way, otherwise you'd need to have a menu selection for the user and then change the material's normal/texture map when the user selects it. Put your materials in an array. In the script you'd need to save a reference to the chair's renderer and on run time change the material of that reference when the user does something. To actually make a complete script we'd need to know how exactly you'd interact with it. (The funcion to change the material will be just a function that you could call from what ever)

Comment

People who like this

0 Show 0 · 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

Answer by ben-rasooli · Nov 09, 2015 at 01:42 PM

Basically you need to have a reference to the material or materials you wanna swap with. To have a reference you can use a public field like this: public Material materialReference; in your script. Then you can assign a material to that field in the inspector. After that, you need to get access to the Renderer component of the object you want to change its material, because that's the component that takes care of material and the appearance of the gameObject. This is the piece of code you need to get to somehow:

 GetComponent<Renderer>().material = materialReference;

and if you just want to change the texture of your material you can do this:

 public Texture2D textureReference;
 
 void OnMouseDown() {
     GetComponent<Renderer>().material.mainTexture = textureReference;
 }

How you going to implement your application to run this line of code is up to you and it's a different story.

Comment

People who like this

0 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 ben-rasooli · Nov 09, 2015 at 01:54 PM 0
Share

This is an example:

 using UnityEngine;
 
 public class MaterialChanger : MonoBehaviour {
     public Material firstMaterialReference; //assign via inspector.
     public Material secondMaterialReference; //assign via inspector.
 
     void Start()
     {
         GetComponent<Renderer>().material = firstMaterialReference;
     }
     
     void OnMouseDown ()
     {
          GetComponent<Renderer>().material = secondMaterialReference;
     }
 
     void OnMouseUp ()
     {
          GetComponent<Renderer>().material = firstMaterialReference;
     }
 }

Attach this script to the gameObject you wanna change its material and then assign two materials to those two public fields via the inspector.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

Playmode don't works 3 Answers

Minor scale issue on play mode 0 Answers

Unity Takes Forever to Exit Play Mode 3 Answers

How to know which material is used by a submesh in script ? 0 Answers

I'm getting a SIGILL error with Unity 2018.2.5 - 6f1. Why is that? 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