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 Apolyon · Apr 19, 2011 at 05:26 PM · gameobjectmaterialchangeon

Change the material on an Object in a script?

How do I give a gameObject a new material when I push a button or hit a trigger? I am new to Unity.

I managed the trigger but not the material change. I don't even know how to put my materials in an array to cycle through them.

To be clear I want to use two different materials I edited in the project tab and assign them to an object in my game according to a parameter. The object is a plane(not aircraft) that moves on the x-y-plane and displays a picture of a soldier holding a rifle moving on the x-y-plane, when he hits a trigger he changes his direction and should change his material or the x-value on the active material. It is for a 2D platformer like Mario. I managed to move him and let him change directions (basicly move backwards) but I want to have the picture from the other side so that the soldier is always facing in the direction he walks.

Sorry for the confusing sentence, I am not a native English speaker so bear with me.

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
14

Answer by novahavocyt · Mar 15, 2018 at 10:35 PM

Hi! If you still need help with this I may be a little help. Here is some code I wrote. Hope it helps you!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class PlayerMaterialSwaper : MonoBehaviour {
    
     public Material Material1;
     //in the editor this is what you would set as the object you wan't to change
     public GameObject Object;
 
     void Start()
     {
          Object.GetComponent<MeshRenderer> ().material = Material1;
     }
 {
Comment
Add comment · Show 4 · 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 Bunny83 · Mar 16, 2018 at 01:32 AM 1
Share

Why don't you just declare your "Object" variable as "Renderer"?

  public $$anonymous$$aterial $$anonymous$$aterial1;
  //in the editor this is what you would set as the object you wan't to change
  public Renderer Object;
 
  void Start()
  {
       Object.material = $$anonymous$$aterial1;
  }

There are more renderers besides $$anonymous$$eshRenderers. Using Renderer will allow any class that is derived from Renderer to be assigned.

avatar image paul-masri-stone · Jun 01, 2020 at 07:06 PM 0
Share

To be more general, Object.GetComponent<$$anonymous$$eshRenderer> could be Object.GetComponent<Renderer>

avatar image unity_AmSezFmN7VbhDQ · Nov 10, 2021 at 01:21 PM 0
Share

What if the Mesh renderer has 3 elements for materials?

avatar image paul-masri-stone unity_AmSezFmN7VbhDQ · Nov 10, 2021 at 02:15 PM 0
Share

If you know there's more than one renderer, then you can use Object.GetComponents<Renderer> and iterate through them, depending on what you want to achieve.

avatar image
0

Answer by JoanMM · Jan 28, 2019 at 01:12 PM

See here: https://answers.unity.com/questions/1373364/how-to-change-object-material-via-script.html

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

6 People are following this question.

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

Related Questions

Changing gameObjects material on trigger? 1 Answer

How to change material of gameobject using C# to a material asset. 2 Answers

Change Materials On Button Press or Hold? 1 Answer

Change material GameObject locally 1 Answer

Player Material Change works on shop preview menu, does not change Player material on other scenes. 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