So I’ve been looking on the internet on how to change a shader at runtime (whenever an object is clicked), and Unity seems to be crashing everytime I try to change the shader.
Here’s my code:
using UnityEngine;
using System.Collections;
public class SetAlive : MonoBehaviour {
public bool alive;
Renderer rend;
Shader deadShader;
Shader aliveShader;
void Start () {
rend = GetComponent<Renderer>();
deadShader = Shader.Find("PurpleShader");
aliveShader = Shader.Find("GreenShader");
}
void OnMouseDown()
{
if (alive)
{
alive = false;
rend.material.shader = Shader.Find("PurpleShader");
}
else
{
alive = true;
rend.material.shader = Shader.Find("GreenShader");
}
}
}
Not really sure what I’m doing wrong for Unity to just crash whenever the object is clicked? I would just switch the gameobject out when clicked instead of changing the shader, but it’s important for me to keep the same object and keep them ordered. If anyone has any insight, thanks!