Question by
CPPninja · Nov 05, 2018 at 09:31 PM ·
playerprefsmaterialsarraysbutton trigger eventsmaterial renderer
Can someone please help me find a logic flaw in my code?
I've written a basic code so that I can have an array of materials that can be accessed when called upon. The idea is the user clicks on a button which changes the material of a block then saves that material's array index using PlayerPrefs. So then in other scenes, the player's material is still changed as it can use PlayerPrefs to access the material at the required array. Right now the program does not change the materials at all when the buttons are pressed (which use an event trigger OnUp press with the program funcytions linked). I really don't understand why this doesn't work. Here's the source code;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class ColourChange : MonoBehaviour
{
public GameObject PlayerChange;
public GameObject PlayerCustom;
public GameObject firstPanel;
public Material[] colors;
int i = 0;
public void OnClick(Button button) {
PlayerPrefs.SetInt("customMat", i);
firstPanel.GetComponent<Renderer>().material.color = colors[PlayerPrefs.GetInt("customMat", 0)].color;
PlayerChange.GetComponent<Renderer>().material = colors[PlayerPrefs.GetInt("customMat", 0)];
PlayerCustom.GetComponent<Renderer>().material = colors[PlayerPrefs.GetInt("customMat", 0)];
}
public void Next(Button button2) {
i = i + 1;
PlayerPrefs.SetInt("customMat", i);
firstPanel.GetComponent<Renderer>().material.color = colors[PlayerPrefs.GetInt("customMat")].color;
PlayerChange.GetComponent<Renderer>().material = colors[PlayerPrefs.GetInt("customMat")];
PlayerCustom.GetComponent<Renderer>().material = colors[PlayerPrefs.GetInt("customMat")];
}
public void Back(Button button3) {
i = i - 1;
PlayerPrefs.SetInt("customMat", i);
firstPanel.GetComponent<Renderer>().material.color = colors[PlayerPrefs.GetInt("customMat")].color;
PlayerChange.GetComponent<Renderer>().material = colors[PlayerPrefs.GetInt("customMat")];
PlayerCustom.GetComponent<Renderer>().material = colors[PlayerPrefs.GetInt("customMat")];
}
}
Comment