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 Raux · Apr 23, 2014 at 03:09 PM · gameobjectmaterialchange

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

I have a square gameobject with a script attached to it in my scene. This gameobject already has a material component called matOne attached to it.

When it's hit by an object, I need the square to change it's material to matTwo, which is inside my project assets folder. I have absolutely no idea on how to do this.

 void OnCollisionEnter(Collision col)
         {
             if (brickType == BrickType.Grass)
             {
                 brickType = BrickType.Dirt;
                 
                 // Code that changes the mesh to a mesh in my assets.
 
                 hitPoints = hitPoints - takeDamage;
                 hitPoints = hitPoints - 1;
             }
         }
 

My guess is that I need to use:

 gameObject.GetComponent<MeshRenderer>().material //some more code

and

 Resources.Load //some more code

But that's all I managed to extract from various other questions and the official documentation.

Comment
Add comment · Show 3
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 anmonta · Apr 23, 2014 at 04:44 PM 0
Share

public $$anonymous$$aterial redBox; //place at top of script and assign the material in the editor this.GetComponent().material = redBox; I think you have the answer already.... So maybe I am missing something

avatar image Raux · Apr 23, 2014 at 07:15 PM 0
Share

That's actually exactly what I needed anmonta, thanks!

avatar image Sisso · Apr 23, 2014 at 07:18 PM 0
Share

@anmonta, please, convert your commant to answer so @Raux could accept it

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Sisso · Apr 23, 2014 at 03:15 PM

Simple read the docs and look its examples

https://docs.unity3d.com/Documentation/ScriptReference/Renderer-material.html

https://docs.unity3d.com/Documentation/ScriptReference/Resources.html

If you didn't understand something in the docs, please ask.

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 FrikFrac · Jul 31, 2016 at 04:40 AM

 public Renderer screen;
 public Material unLockedMat;

 void LazerDeactivation()
 {
     lazer.SetActive(false);

     screen.material = unLockedMat;
     GetComponent<AudioSource>().Play();
 }

I had same problem I created a public Renderer variable for the screen and for the material. It works but remember the Renderer must be the exact object which contains the material you wish to change not the parent object.

I hope this helps.

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

23 People are following this question.

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

Related Questions

Material doesn't have a color property '_Color' 4 Answers

Changing gameObjects material on trigger? 1 Answer

Change the material on an Object in a script? 2 Answers

Load Material to GameObject 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 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