Hello, is there any posibility to change the material of the player when he hits a trigger? The object that I want to change material is a child of the player, but it seems that my script doesn’t work at all. This is what I came up with:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Security.Cryptography;
using UnityEngine;
using Random = UnityEngine.Random;
public class ChangeColor : MonoBehaviour
{
public Material[] material;
Renderer rend;
void Start()
{
rend = GetComponent<Renderer>();
rend.enabled = true;
int i = Random.Range(0, 6);
rend.sharedMaterial = material*;*
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject.tag == “ColorChange”)
{
int i = Random.Range(0, 4);
rend.sharedMaterial = material*;*
}
}
}
First, this script have to be attached to the player GameObject.
Try this:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics; using System.Security.Cryptography;
using UnityEngine; using Random = UnityEngine.Random;
public class ChangeColor : MonoBehaviour {
public Material[] material;
public GameObject ChildGameObject;
Renderer rend;
void Start()
{
ChildGameObject = gameObject.transform.GetChild.gameObject;
rend = ChildGameObject.GetComponent<Renderer>();
rend.enabled = true;
int i = Random.Range(0, 6);
rend.sharedMaterial = material*;*
}
void OnTriggerEnter (Collider other)
{
if (other.gameObject.tag == “ColorChange”)
{
int i = Random.Range(0, 4);
rend.sharedMaterial = material*;*
}
}
}
Here you can see that I select the child of the player and assign to rend the renderer of the child.
Now, if the player is hitting the trigger (with “ColorChange” tag), the child gameObject should change of color.
@Artik2442 This thread worked for me too, but I want to add some time before the Collider changes color when hovering over it.