1

Goal

I created a shader graph that I use in a material "TestMaterial". I have a game Object with a sprite renderer component with a default material property.

At runtime when user click on a simple button I want to change my gameObject > Sprite renderer > Material property "default" with my new material "TestMaterial".

Context

I use Universal Render Pipeline and Unity 2019.4.12.f1. My code worked well before I use the function Edit > render pipeline > universal render pipeline > 2D renderer > Update project to 2d renderer.

Tests I made My "TestMaterial" work well in editor and also at runtime when my gameobject use directly this material. "TestMaterial" doesn't work only if I update material in my script, my gameObject keep old default material.

Code

[SerializeField] Material testMaterial;
[SerializeField] Material defaultMaterial;

public void UpdateToDefaultMaterial()
{
   this.gameObject.GetComponent<SpriteRenderer>().material = defaultMaterial;
}

public void UpdateToTestMaterial()
{
   this.gameObject.GetComponent<SpriteRenderer>().material = testMaterial;
}

Screenshot Unity Editor

Bishok
  • 11
  • 1
  • 4

2 Answers2

0
  1. Made sure you can replace it properly in the editor
  2. I don't think sprite editors can use materials. Use a different kind of object to put the material on
  • It works well in editor I can replace material on inspector and my material with shader graph is Ok even when I run the game. But if i change material in my script at runtime with my method "UpdateToTestMaterial" it doesn't work anymore. – Bishok Apr 07 '21 at 13:30
0

Ok I solved my problem using an animator with a looping animation in which I change my material :

GameObject Sprite renderer Component

Animation where I update material

I still don't know why my animation works and not my code but It do the job... I will be happy if someone finds the answer for my culture...

Thanks

Bishok
  • 11
  • 1
  • 4