Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by Jack-Mariani · Aug 23, 2015 at 08:50 AM · c#shadermaterialspriterenderer

How to get the spriterender default material in the code

Hi. I'd like to change the material of my spriterenderer if the player select a unit.

I created my shader, and my material.

So if I want to set the shader I simply add:

 spriteRenderer.material = myMaterial;

How can I reset it to default material?

I tried by storing it afterhand by adding:

 Material defaultSpriteMaterial = spriteRenderer.material;
 spriteRenderer.material = myMaterial;

Then I would simply readd it after player deselects the unit. spriteRenderer.material = defaultMaterial;

But it is not working.

Is it possible to access the "Sprites_Default" material in other ways?

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

Answer by matmel · Aug 23, 2015 at 04:35 PM

Hi there. What about assigning the deafult material to an variable in editor. I mean something like this:

 public Material deafultMaterial;
 public Material yourMaterial;

Simply click on the circle on the right side of your instance in the inspector and add deafultmaterial to it. I think this is a easier way to reset the material and add yourMaterial:

     void ResetMaterial()
     {
         GetComponent<SpriteRenderer>().material = deafultMaterioal;
     }
 void ChangeMaterial()
 {
 GetComponent<SpriteRenderer>().material = yourMaterial;
 }
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 Jack-Mariani · Aug 23, 2015 at 05:01 PM 0
Share

In that case I have to do the following: - Create the material (using the shader sprite-default) - $$anonymous$$anually assigning the material to each script, or add the material to a singleton and let all elements take it from there.

I'm currently doing that, but I was hoping to find a solution just via code, without adding any more materials.

avatar image matmel · Aug 23, 2015 at 07:02 PM 0
Share

Oh yes, i see. $$anonymous$$aybe you will find Shader.find usefull? Take a look, this allows you change a material under runtime http://docs.unity3d.com/ScriptReference/Shader.Find.html

avatar image maccabbe · Aug 23, 2015 at 09:13 PM 0
Share

You could also set default$$anonymous$$aterial to the material of the object on start

 void Start()
 {
      default$$anonymous$$aterial = GetComponent<SpriteRenderer>().material;
 }
avatar image Jack-Mariani · Aug 24, 2015 at 07:09 AM 6
Share

Not sure why, but if I tried to get the material from the start (or from the awake, or anywhere else, before assigning it), it returns a non working material (all sprites become transparent).

I used the Shader Find and it works properly.

 default$$anonymous$$at = new $$anonymous$$aterial(Shader.Find("Sprites/Default"));

Thank you very much!

avatar image Shadoninja Jack-Mariani · May 02, 2018 at 12:46 AM 1
Share

YOU were the only one to have the information I need. Thankkkkk youuuuu!

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

28 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

How to apply a shader to all materials inside a gameobject 1 Answer

Painting materials onto meshes at runtime? 1 Answer

Image Effect lighting 0 Answers

Can only the material set on a SpriteRenderer be affected by a unique 2D light ? 0 Answers

Fragment Shader not applyable at runtime? 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