Materials changed via script turn pink
I am trying to write a script that allows the player to switch between which group of objects (red and blue blocks) can be passed through. There's also a function that's meant to change the material of the boxes to another color to help the player tell which state the they are in.
However, the materials end up becoming pink once I go into test mode. I'm using the Universal Render Pipeline for this project, but I haven't been able to find any tutorials and what to do in order to fix the pink material issue when changing them through a script. It is also possible that the script isn't calling upon the right materials in the first place, but I am still very new to C# so I'm not certain either way. I have attached a sample of the script in question below.
{
public Material OffBlueBlock, OnBlueBlock, OffRedBlock, OnRedBlock;
private GameObject[] blueStuff;
private GameObject[] redStuff;
void Start()
{
redStuff = GameObject.FindGameObjectsWithTag("Reds"); //Affects red blocks
foreach (GameObject Reds in redStuff)
{
Reds.GetComponent<BoxCollider>().enabled = true;
Reds.GetComponent<Renderer>().material = OnRedBlock;
}
blueStuff = GameObject.FindGameObjectsWithTag("Blues"); //Affects blue blocks
foreach (GameObject Blues in blueStuff)
{
Blues.GetComponent<BoxCollider>().enabled = false;
Blues.GetComponent<Renderer>().material = OffBlueBlock;
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
if (Hit == true)
Hit = false;
else
Hit = true;
if (Hit == true)
{
blueStuff = GameObject.FindGameObjectsWithTag("Blues"); //Affects blue blocks
foreach(GameObject Blues in blueStuff)
{
Blues.GetComponent<BoxCollider>().enabled = false;
Blues.GetComponent<Renderer>().material = OffBlueBlock;
}
redStuff = GameObject.FindGameObjectsWithTag("Reds"); //Affects red blocks
foreach (GameObject Reds in redStuff)
{
Reds.GetComponent<BoxCollider>().enabled = true;
Reds.GetComponent<Renderer>().material = OnRedBlock;
}
}
else
{
blueStuff = GameObject.FindGameObjectsWithTag("Blues"); //Affects blue blocks
foreach (GameObject Blues in blueStuff)
{
Blues.GetComponent<BoxCollider>().enabled = true;
Blues.GetComponent<Renderer>().material = OnBlueBlock;
}
redStuff = GameObject.FindGameObjectsWithTag("Reds"); //Affects red blocks
foreach (GameObject Reds in redStuff)
{
Reds.GetComponent<BoxCollider>().enabled = false;
Reds.GetComponent<Renderer>().material = OffRedBlock;
}
}
}
}
Your answer
