- Home /
Changing gameobject material color makes object disappear from view. How can I fix this?
I have this bit of code that says:
bool bt1 = false;
public GameObject Stripe1;
public bool setMeOnly() {
bt1 = bt2 = bt3 = bt4 = bt5 = bt6 =bt7 = false;
return true;
}
void OnGUI() {
bt1=GUI.Toggle(new Rect(300, 85, 100, 30), bt1, "");
if(bt1) bt1=setMeOnly();
if (bt1 == true) {
Stripe1.renderer.material.color = Color.red;
Stripe1.renderer.enabled = true;
}
Howver, instead of rendering the stripe1 color to red, the game object stripe 1 disappears from the scene (although it's listed on the left panel in the hierarchy).
I'm not sure why, there are no errors in the log.
Although when I manually change it to red when the game is in session (scene is played), I get this error that says all the duplicate objects to red:
rc.right != m_GfxWindow->GetWidth() || rc.bottom != m_GfxWindow->GetHeight()
GUI Window tries to begin rendering while something else has not finished rendering! Either you have a recursive OnGUI rendering, or previous OnGUI did not clean up properly.
Here's a bit of background on what I'm trying to do. I have duplicate objects called "Cube". They all contain the same material, color but positioned different on the scene. When I click on a toggle button next to the game object cube (stripe), the stripe should turn from default gray to red if the toggle button = true. However, instead of turning red, the object disappears from the scene. That is stranger is that when I click on Cube "Stripe1" on the hierarchy, I can see that it says "Gray (instance) and the color is now changed to red" even though the object disappeared.... This is strange since the Mesh Renderer is checked and the cube is checked off as well in the inspector.
When it runs, and you can see it in the inspector, what does it say the material color is?
Answer by Leckofunny · Apr 16, 2014 at 10:54 PM
Its a problem of transparency. For some reason Color.red has a transparency value of a = 0 i.e. completely transparent, which is why your stripe1 disappears. You can either set the Color by creating a new one: new Color(someRedValue,someGreenValue,someBlueValue,1f)
or if you have a variable for the desired color (for example myColor) then you can just set the transparency value to 1: myColor.a = 1f;