- Home /
Material Instance doesn’t work correctly (involve Array)
-What I want to do (goal): I want to hide the wall when the player enter a room, hide all things on top of that room (like 2nd floor) -How I approached it: reduce the alpha of the 1st floor and disable renderer of the 2nd floor (&EverythingOnThere) by: + Create a Bool isInRoomB to check wether the character is in a room or not (using RayCast):
private bool checkRoof(Vector3 Origin)
{
Origin += Vector3.up * 3;
RaycastHit hit;
Ray checkRoofRay = new Ray(Origin, Vector3.up);
Debug.DrawRay(checkRoofRay.origin, checkRoofRay.direction * 10, Color.red);
if (Physics.Raycast(checkRoofRay, out hit))
{
currentRoof = hit.transform.gameObject;
if (hit.collider.tag == "Roof")
{
Debug.DrawRay(checkRoofRay.origin, checkRoofRay.direction * 90, Color.grey);
return true;
}
}
return false;
}
I create an array to store the material when the player first go to the room, When the character enter, “isInRoomB” is set to “true” and the Renderer of the Ceiling get all of it’s parent’s renderer. (the 2ndFloor and Everything on there) then hide the 2nd floor and make the 1st transparent
The Format of this code just doesnt work correctly on here, pls look over it.
private void ChangeTransparent(bool isInRoomB, GameObject currentRoof) { Debug.Log("inRoomB = " + inRoomB); if (isInRoomB) { GameObject currentWall = currentRoof.transform.parent.gameObject; renArr = currentWall.GetComponentsInParent();
for (int i = 0; i < renArr.Length; i++) { if (i == 0) { m_objectColor.a = seeThroughRate; renArr[i].material.color = m_objectColor; }//invisible current floor and else { renArr[i].enabled = false; }//disable those that's on top } } else { for (int i = 0; i < renArr.Length; i++) { m_objectColor.a = 1.0f; renArr[i].material.color = m_objectColor; m_ObjectRenderer = renArr[i]; renArr[i].enabled = true; } } }
When the character isn’t in that room anymore, the RendererArray is still the same bc the ray hasn’t contact with another ceiling yet. It then return the alpha to 1.0f to not make it transparent anymore and enable the renderer of the 2nd floor & everything on it
The problem: when character exits the room, everything should change to normal like before. However every materials has been altered to be the instance material that looks like the outer wall. I have no Idea why. Thanks in advance. Below is the img:
*For someone who're thinking about “renderer[i].material = normal & transparent” It doesn’t work. I tried it: by switching Material (using 2 arrays to store and switching material) & it just has the same problem