I have a scene named “Shop” in that scene I have a script that changes the material of my “Player” prefab via buttons OnClick event. Everything works fine. BUT when I build the game everything in the OnClick event works EXCEPT that it doesn’t change the material
Here is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ChangeMaterials : MonoBehaviour
{
public bool isOwnedRed = false;
public bool isOwnedBlue = false;
public Text screenMessage;
public Material[] materials;
public Player playerScript;
public GameObject player;
public MeshRenderer playerRenderer;
void Start()
{
playerRenderer = player.GetComponent<MeshRenderer>();
}
public IEnumerator ScreenMessage ()
{
screenMessage.enabled = true;
yield return new WaitForSeconds(1.5f);
screenMessage.enabled = false;
}
public void DefaultWhite()
{
screenMessage.text = "Material changed";
playerRenderer.sharedMaterial = materials[0];
StartCoroutine(ScreenMessage());
}
public void Red()
{
float cost = 15f;
if (playerScript.coins >= cost && isOwnedRed == false)
{
Debug.Log("color RED ");
playerRenderer.sharedMaterial = materials[1];
playerScript.coins -= cost;
playerScript.Save();
playerScript.Load();
screenMessage.text = "You bought RED material for 15 coins!";
StartCoroutine(ScreenMessage());
isOwnedRed = true;
}
else if(isOwnedRed == true)
{
screenMessage.text = "Material changed";
playerRenderer.sharedMaterial = materials[1];
StartCoroutine(ScreenMessage());
}
else
{
screenMessage.text = ("NOT ENOUGH COINS");
StartCoroutine(ScreenMessage());
}
}
public void Blue()
{
float cost = 15f;
if (playerScript.coins >= cost && isOwnedBlue == false)
{
playerRenderer.sharedMaterial = materials[2];
playerScript.coins -= cost;
playerScript.Save();
playerScript.Load();
screenMessage.text = "You bought BLUE material for 15 coins!";
StartCoroutine(ScreenMessage());
isOwnedBlue = true;
}
else if (isOwnedBlue == true)
{
screenMessage.text = "Material changed";
playerRenderer.sharedMaterial = materials[2];
StartCoroutine(ScreenMessage());
}
else
{
StartCoroutine(ScreenMessage());
screenMessage.text = ("NOT ENOUGH COINS");
}
}
}