The answer to the boolean issue provided by @Mario below solved the main issue as well
The code in the original post (below the line) had an unrelated problem ('==' instead of '=') but in fixing that I've realized that I am having an issue with the string name of 'cubeRend.material' which includes the additive Instance after it and therefore doesn't seem to be counted as logically equal to the string name of 'material[0].'
I don't THINK that is the issue behind the resetting problem however, because I did find a question about a similar resetting problem on the Unity answer forum here: https://answers.unity.com/questions/1303925/ui-buttons-images-resets-after-scene-reloads-scrip.html
Unfortunately no one provided any responses to that question which I could try to apply in this case. I will try to sort out my equivalency problem and then update with the improved code
I'm trying to make a UI button change the colors on a cube on every click. The materials are in an array. In the start function, I set the initial condition of the cube's renderer to material[0]. In the ChangeCubeColor function (which is referred to on the UI button's inspector), I use a simple if/else statement to check which material is currently assigned to the cube. On clicking the button, Unity seems to reset the material back to the original condition and then invisible to the eye follow the if/else instructions to set the color to the 2nd color in the array. The affect is that on the first time you play, the button will change the color, but every time after that, the color is stuck on the second color.
Here is my code. I apologize for all of the debug statements. I was trying to figure out when the state was or was not changing. I've also included a screenshot of my console upon first play and the first 3 clicks of the button. Lastly, the code with the debug statements removed for clarity.
Thanks in advance for any ideas.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeCubeColor : MonoBehaviour
{
public Material[] material;
Renderer cubeRend;
void Start()
{
cubeRend = GetComponent<Renderer>();
cubeRend.material = material[0];
Debug.Log(cubeRend.material);
}
public void CubeColorChange()
{
if(cubeRend.material = material[0])
{
Debug.Log("cubeRend.material = material[0]");
cubeRend.material = material[1];
Debug.Log("Make 1: "+cubeRend.material);
Debug.Log("cubeRend.material = material[1]");
}
else if (cubeRend.material = material[1])
{
Debug.Log("cubeRend.material = material[1]");
cubeRend.material = material[0];
Debug.Log("Make 0: " + cubeRend.material);
Debug.Log("cubeRend.material = material[0]");
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeCubeColor : MonoBehaviour
{
public Material[] material;
Renderer cubeRend;
void Start()
{
cubeRend = GetComponent<Renderer>();
cubeRend.material = material[0];
}
public void CubeColorChange()
{
if(cubeRend.material = material[0])
{
cubeRend.material = material[1];
}
else if (cubeRend.material = material[1])
{
cubeRend.material = material[0];
}
}
}
Try this way. This work for 2 materials if you want more you can use an int index and update the index without use if for each material
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeCubeColor : MonoBehaviour
{
public Material[] material;
public bool state;
Renderer cubeRend;
void Start()
{
cubeRend = GetComponent<Renderer>();
cubeRend.material = material[0];
//state = false is material 0 //state = true is material 1
state = false;
}
public void CubeColorChange()
{
//Change the state to the other state
state = !state;
cubeRend.material = (state) ? material[1] : material[0];
}
}
EDIT 1:
Here is the variation with index for more materials
using UnityEngine;
public class ChangeCubeColor : MonoBehaviour
{
public Material[] material;
public int index;
Renderer cubeRend;
void Start()
{
cubeRend = GetComponent<Renderer>();
index = 0;
cubeRend.material = material[index];
}
public void CubeColorChange()
{
//Increase index
index = (material.Length - 1 > index) ? index + 1 : 0;
cubeRend.material = material[index];
}
}