- Home /
How to change the material color of an Instatiated prefab when the player enters the collision area and presses a button?
I've used video tutorials and the unity documentation for everything in my game. Even though it mostly all adds up to more questions, the result are well worth it. I need additional help with this issue.
First off I receive no error codes. the function of my script is : As the player enters the box collider collision area of the gameobject and has pressed the right mouse button, change the set default color of the gameobject to a new color. Because This has not worked I also have not been able to set the conditions for ONmouseUP. Which would be keep the new set color the same after release.
The script is attached to the gameobject and not the player gameobject. Please tell me if I haven't given enough info. Thank you. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ChangeColor : MonoBehaviour {
public GameObject idelBlock; //REF to bloack to be changed
public Renderer rend; //REF to the render to change colors
private Color startColor; //REF to the defualt color of Instantiated block
//REF to player so Block knows what is trying to change its color
public GameObject player;
public float distance; //the colision range to active
public bool inActivationRange = false; //is in range??? yes or no
void Start()
{
player = GameObject.FindWithTag("Player");
if( player == null)
{
Debug.Log("Player is not tagged");
}
idelBlock = this.gameObject;
rend = GetComponent<Renderer>();
startColor = rend.material.GetColor("_Color");
}
void Update()
{
distance = Vector3.Distance(idelBlock.transform.position, player.transform.position);
}
//if player ckicks R/mouse and the distance is correct fro activation range
//change the defualt "start color" to different color.
//if cliked a again <= 4secs color cant be changed until after 4sec
public void OnMouseDown()
{
if (Input.GetMouseButtonDown(1))
{
if(distance <= 1.5f)
{
inActivationRange = true;
startColor = GetComponent<Renderer>().material.color = Color.red;
}
}
}
//when the the click is released keep color as the new set color
//when click is up within 4sec again Print "You have to wait 4Seconds"
public void OnMouseUp()
{
}
}