Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 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
0
Question by consumedman · May 10 at 04:06 PM · renderingmaterial

Changing material properties on one object changes all others

I have a fadeout script to change the material properties when something blocks the screen. I want the objects to fade individually but when a new object fades, all the others also start the fade again or start fully faded out immediately.

The Docs say changing material properties using Renderer.material creates a new instance of the material, but the behaviour I see points to them still being the same instance.

The material is a simple unlit shadergraph and here's the Coroutine I use to fade the object:

 IEnumerator TransitionTransparency() {
     _renderer.material = _transparentMat;
 
     float transition = 1;
     while (transition > 0) {
         _renderer.material.SetFloat("_Transition", transition);
 
         transition -= Time.deltaTime / _transitionTime;
         yield return null;
     }
 
     Destroy(_renderer.material);
     _renderer.material = _transparentMat;
       
     yield break;
 }




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

Answer by HeilBoomer · May 10 at 05:51 PM

I'd recommend Material Property Blocks. It is a class that you can set to almost every renderer and it will override the renderer's shader properties.

For Instance, here is a script that I wrote for tweaking the "_Fade" property using DOTween.

 using DG.Tweening;
 using UnityEngine;
 using UnityEngine.Events;
 
 public class Interactable : MonoBehaviour, IRaycastEnter
 {
     [SerializeField] protected MeshRenderer mR;
     protected MaterialPropertyBlock mPB;
     protected Vector3 StartScale;
 
     protected float tw = 0;
 
     protected virtual void Awake()
     {
         mPB = new MaterialPropertyBlock();
     }

      // This isn't the whole script but I think that would be enough to show how it works.
     public virtual void OnRaycastEnter()
     {
         if (!interactable) return;
 
         //throw new System.NotImplementedException();
         if (flashTW != null) flashTW.Kill();
         flashTW = DOTween.To(() => tw, x => tw = x, 1, .22f)
                 .OnUpdate(() =>
                 {
                     mR.transform.localScale = StartScale * (1 + (tw * scaleMultiplier));
                     // This is the Material Property Part, first you get the property block from the renderer.
                     mR.GetPropertyBlock(mPB);

                     //Then you tweak the values in the Material Property Block
                     mPB.SetFloat("_Fade", tw);

                     //Finally you set the property block of the renderer
                     mR.SetPropertyBlock(mPB);
                 });
     }
 }




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 consumedman · May 10 at 06:29 PM 1
Share

Thanks, Tried it out and this solution works even without setting the override. I rewrote my code to use DOTween in the meantime (forgot I had it in the project) and for a single property it seems simpler. I suppose the upside of using your solution (apart from being what unity is pushing towards, I guess) is that fewer draw calls are made, so it definitely has merit when many objects are changed. Anyways, thanks for pointing out a feature I was unaware of.

avatar image
0

Answer by consumedman · May 10 at 05:37 PM

So after trying out a few things I found the solution. Leaving it here for anyone having a similar issue in the future.

The problem is with how shadergraph shaders handles non-exposed properties by default. If you want to have a non-exposed property that can be changed for every material instance you have to enable "Override property declaration" and set "Shader Declaration" to "per material" in the property settings.

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

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

Related Questions

Changing colors of a material 1 Answer

Is there any way to test to see if the project is uing the URP/HDRP? 2 Answers

Render a object on top of another. 2 Answers

How to FadeIn 3dObject using alpha color? 1 Answer

How can i get my quad to only render my texture without stretching it? 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