- Home /
Changing sprite renderer material cause sprite disappear
I created my own shader using shader graph to cover my specific problem. I need to create some kind of FOV for my main camera. Shader take global position of the camera and offset global position of mask on material assigned to multiple sprites. That let me insert few sprites on scene and mask will always be in same global position as camera. That works just fine, and I am happy with the result, but... Now I need to have couple of objects with this material always "visible". I was trying a lot of ways to do this, but always something goes wrong. Whatever I tried it finished as sprite disappear.
1) I was trying to use Branch node in shader graph to turn on and off mask, based on property changed by C# script (MaterialPropertyBlock) for single object. Sprite disappear whenever I want to switch its state.
2) Next time I tried change radius property via C#, same problem, but if I am passing Camera.main.transform.localScale.x to this property it is OK, and i can adjust radius using camera scale.
3) Finally, i was trying to just switch material on single object via script...disappear. It is disappearing even I manualy switch material on runtime.
There are two more things to say:
All this sprites are instantiate by some kind of generator, maybe problem is placed there, but it is just normal instantiate. I wrote small script to instantiate same prefab, and this one works fine. I can change material manualy and it works.
Second one, I find out that when I am changing material on sprite or properties, sprites wireframe is changing. If normal sprite is like two trinagles, that after change material it is disappear. Only if i select object, i can see rectangle outline around it.
I am going to go through my generator scripts tomorow, maybe i`ll find something, but it is looking weird.
Apologize my english, if something isn`t understandable i can try to give you more context.
Answer by skwr · May 05, 2019 at 05:26 PM
I rebuild my generator to Instantiate every object from same place in code, and it seems to fix problem.
edit: it isn`t fix problem, but I found what exactly cause it. Everything works fine until I change size of object (transform.localSize) from script. First I thought that is problem, but it isn`t. I am just stupid and set localSize as new Vector3(sizeX, sizeY, 0). Zero on Z axis cause disappearing my object from scene when i pass values to shader parameters.
I didn`t work on Unity for long time, past in time every 2D object has 0 depth in my small projects and everything works fine. Looks like something change ;)
At least I rebuild my generator, and now it works better, faster and so on. Nothing bad happend.
Hope that it can help someone in future.
Your answer
Follow this Question
Related Questions
Why is my sprite disappearing when I make a new material and add the Sprites-Default shader? 1 Answer
Limit Amount of Light Applied to Sprite 0 Answers
Problem to solve Z-Fighting with Offset shader, how can I do it works? 2 Answers
Sprites not working with Depth of Field Post Processing 0 Answers
How to make transparent shader (2D)? 1 Answer