Problem working with Material (Shader and texture problem)

Hi all,

I’ve been trying to work with Unity2D today and I decided I wanted to write a shader with a material to add a transparent (dynamic) white background to a sprite (torch). I added a way to animate the sprite and that worked fine with a default material and shader. When I went to make my own material and shader the texture is just a purple rectangle. I am not really experienced in this so excuse the code.

I’m looking to replacing pixels that have an alpha of 0.

So any help you can give me would be great.

This is the shader:

Shader "Custom/TorchShader" {
    Properties {
        _MainTex ("Main Texture", 2D) = "white" { }
        _Color ("Background Color", Color) = (1, 1, 1, 0.2)
    }
    SubShader {
        Pass {
            Name "ColorReplacement"

            CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag

                struct Vert {
                    float4 pos : SV_POSITION;
                    float2 uv : TEXCOORD0;
                };

                Vert vert (appdata_tan v) {
                    Vert o;
                    o.pos = v.vertex;
                    o.uv = v.texcoord.xy;
                    return o;
                }

                sampler2D _MainTex;
                float4 _Color;

                float4 frag(Vert i) : COLOR {
                    float4 result;
                    if (result.a == 0) {
                        result.rgb = _Color.rgb;
                        result.a = _Color.a;
                    } else {
                        result.rgb = _MainTex.rgb;
                        result.a = _MainTex.a;
                    }
                    return result;
                }

            ENDCG
        }
    }
    Fallback "Diffuse"
}

And here is the Sprite animating class that also changes the materials texture dynamically

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpriteAnimation : MonoBehaviour {
    private SpriteRenderer _spriteRenderer;
    private Sprite[] _sprites;
    private float _deltaTime = 0;
    private int _frameToSelect = 0;
    private bool printed = false;

    public string assetName;
    public float frameSeconds = 1;

    // Use this for initialization
    void Start () {
        _spriteRenderer = GetComponent<SpriteRenderer> ();
        _sprites = Resources.LoadAll<Sprite> (assetName);
    }
   
    // Update is called once per frame
    void Update () {
        _deltaTime += Time.deltaTime;
        while (_deltaTime > frameSeconds) {
            _deltaTime -= frameSeconds;
            _frameToSelect++;
            _frameToSelect %= _sprites.Length;
        }
        _spriteRenderer.sprite = _sprites [_frameToSelect];
        _spriteRenderer.material.SetTexture ("_MainTex", (Texture)_sprites[_frameToSelect].texture);
    }
}

one problem for sure, in your frag() function, you’re referencing result before it’s been initialized to anything. i think instead of if (result.a == 0) { you wanted if (_MainTex.a == 0) {