I have a scene where the Player obtains the material from other GameObjects on pressing key ‘E’.
Script:-
public Renderer rend;
public Material defaultMaterial;
private float distance = 3;
void Update()
{
Ray ray = new Ray(transform.position + transform.up * 1, transform.forward);
RaycastHit hit;
Debug.DrawRay(transform.position + transform.up * 1, transform.forward, Color.green);
if(Physics.Raycast(ray, out hit, distance))
{
if(hit.collider.tag == "Enemy")
{
Renderer otherRend = hit.collider.GetComponent<Renderer>();
if(Input.GetKeyDown(KeyCode.E) && otherRend.material != defaultMaterial)
{
rend.material = otherRend.material;
otherRend.material = defaultMaterial;
}
}
}
}
After the player obtains the material from the object, the material of that object is set to default material which I can specify within Inspector. Once that material is set to default, the Player should no longer be able to get the material of that object. In short, I don’t want player to get default material, but if its not default, then it should be able to get it. That’s why I wrote the condition so that it should check if otherRend has default material or not.
if(Input.GetKeyDown(KeyCode.E) && otherRend.material != defaultMaterial)
But its not working as Player obtains the material, no matter what. What changes can be made? Any
help is appreciated.