- Home /
Why when changing the doors colors it's not changing anything ?
The script should change the color of the doors from red to green when unlocked:
Using a break point I see it's getting to the line:
rend.material.color = unlockedColor;
But never change the color to green.
This is a screenshot example of one of the doors:
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class DoorsLockManager : MonoBehaviour
{
public bool lockDoors = false;
private Renderer rend;
private Shader unlitcolor;
private List<GameObject> DoorShieldFXLocked = new List<GameObject>();
private List<HoriDoorManager> _doors = new List<HoriDoorManager>();
private void Start()
{
DoorShieldFXLocked = GameObject.FindGameObjectsWithTag("DoorShield").ToList();
var doors = GameObject.FindGameObjectsWithTag("Door");
foreach (var door in doors)
{
_doors.Add(door.GetComponent<HoriDoorManager>());
}
for (int i = 0; i < _doors.Count; i++)
{
if (lockDoors == true)
{
LockDoor(i);
ChangeColors(Color.red, Color.green, i);
}
else
{
UnlockDoor(i);
ChangeColors(Color.red, Color.green, i);
}
}
}
private void Update()
{
}
private void ChangeMaterialSettings()
{
unlitcolor = Shader.Find("Unlit/ShieldFX");
rend.material.shader = unlitcolor;
//rend.material.SetFloat("_Metallic", 1);
}
private void ChangeColors(Color32 lockedColor, Color32 unlockedColor, int index)
{
rend = DoorShieldFXLocked[index].GetComponent<Renderer>();
ChangeMaterialSettings();
if (lockDoors == true)
{
rend.material.color = lockedColor;
}
else
{
rend.material.color = unlockedColor;
}
}
public void LockDoor(int doorIndex)
{
_doors[doorIndex].ChangeLockState(true);
}
public void UnlockDoor(int doorIndex)
{
_doors[doorIndex].ChangeLockState(false);
}
}
Answer by WarmedxMints · Mar 11, 2019 at 07:50 AM
You need to get the material, modify it and then apply it to the renderer. Also, you could change DoorShielfFXLocked to a list of renderers to and save yourself the cpu cycles of finding the renderer each time.
private void Start()
{
//Put this in start and remove the ChangeMaterialSettings Method
unlitcolor = Shader.Find("Unlit/ShieldFX");
//...
}
private void ChangeColors(Color32 lockedColor, Color32 unlockedColor, int index)
{
var renderer = DoorShieldFXLocked[index].GetComponent<Renderer>();
var material = renderer.material;
material.shader = unlitcolor;
if (lockDoors == true)
{
material.color = lockedColor;
}
else
{
material.color = unlockedColor;
}
renderer.material = material;
}
Your answer
Follow this Question
Related Questions
How can I add a MeshRenderer component with all properties to a GameObject by script ? 1 Answer
How can i using a break point if a gameobject have a collider after added to it ? 1 Answer
What is the Input Key for the Steam Vive triggers? 1 Answer
How can i sum the number of generated nodes each time clicking the checkbox ? 1 Answer
Unity Navmesh agent patrol and chase player script issues. 0 Answers