Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Game Development

Chris Kopcow
Chris Kopcow
2,852 Points

Changing render mode in Unity during runtime

Hi! I'm in the process of making my first game, and I'm running into an issue I can't solve.

The scene I'm working on is a 3D room with a top-down perspective, using a 2D sprite as the player. Since the front wall of the room will obscure the player when you walk to the bottom of the screen, I want to make the wall change to the Fade render mode when you walk toward it to make the player visible, while making it clear the wall is still there.

Looking through Unity forums and documentation, it seems you enable the keyword "_ALPHABLEND_ON" to use the Fade render mode, but when I did this, it didn't work. (Even worse, other variations of my code caused my sprite to clip through the wall and fall into the void below.)

Here is my current code:

public class FadeOut : MonoBehaviour {

    [SerializeField]
    private Transform player;
    private Material standardShader;

    // Use this for initialization
    void Start ()
    {
        standardShader = GetComponent<Renderer>().material;
    }

    // Update is called once per frame
    void Update ()
    {
        if (player.position.z >= 7.78f)
        {
            standardShader.EnableKeyword("_ALPHABLEND_ON");
        }
    }
}

I've looked everywhere online, and I've tried a few of the solutions other people have offered, but nothing seems to work. This seems like it should be easier to execute than it is, considering how easy it is to modify everything else in a material.

Can anyone help?