material.SetColor causing Hidden/InternalErrorShader
First, I'm a beginner, and thanks in advance for any help you can offer.
I'm attempting to change the color of a cube (floor tiles) based on the color status of the player when he collides with them. The code I've written seems to accomplish that, except for one thing. When I press the play button, and run over a floor tile, the cube changes to a bright pink color, and the shader on the object changes from "Standard" to "Hidden/InternalErrorShader". The reason I say that my code seems to work is that if I change the shader back to Standard while in play mode, the cube will display the proper color.
Script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ColorChange : MonoBehaviour {
bool activeColorWhite = true;
bool activeColorBlue = false;
bool activeColorGreen = false;
bool activeColorYellow = false;
bool activeColorRed = false;
bool activeColorMagenta = false;
public int counter = 0;
void OnCollisionEnter(Collision collisionInfo)
{
Renderer rend = GetComponent<Renderer>();
if (collisionInfo.collider.tag == "Player" && activeColorBlue == true)
{
rend.material.shader = Shader.Find("_Color");
rend.material.SetColor("_Color", Color.blue);
}
else if (collisionInfo.collider.tag == "Player" && activeColorGreen == true)
{
rend.material.shader = Shader.Find("_Color");
rend.material.SetColor("_Color", Color.green);
}
else if (collisionInfo.collider.tag == "Player" && activeColorYellow == true)
{
rend.material.shader = Shader.Find("_Color");
rend.material.SetColor("_Color", Color.yellow);
}
else if (collisionInfo.collider.tag == "Player" && activeColorRed == true)
{
rend.material.shader = Shader.Find("_Color");
rend.material.SetColor("_Color", Color.red);
}
else if (collisionInfo.collider.tag == "Player" && activeColorMagenta == true)
{
rend.material.shader = Shader.Find("_Color");
rend.material.SetColor("_Color", Color.black);
}
// the rest is an update function that determines which bool is true based on the value of counter
}
Answer by Avadora · Sep 05, 2018 at 05:24 AM
I fixed this by changing the color in a different way.
Instead of rend.material.shader = Shader.Find("_Color"); rend.material.SetColor("_Color", Color.blue);
I used gameObject.GetComponent().material.color = new Color(0, 0, 1, 1);
Even though this has been solved, if anybody knows what may have been causing the error I was getting with the previous code, I'm interested to know!
Your answer
Follow this Question
Related Questions
Whenever I try to enter playmode after creating a script, a compiler error pops up 0 Answers
error CS1525: Unexpected symbol `)', expecting `(' 1 Answer
How to make a number higher than Quintillion? (1000000000000000000) 1 Answer
Subscribing to an event on a gameobject that was moved to the scene wont go through 2 Answers
how can I get my prefab to have a different material on instantiation 1 Answer