How can I change the colour of an object when it collides with a user in a VR world. ,How can I make an object change colour upon collision with a user in a VR world?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class SilverFernScript : MonoBehaviour {
void Start()
{
Renderer = GetComponet<Renderer>();
private void OnPreRender()
{
renderer.sharedMaterial = Material[0];
}
private void OnCollisionEnter(Collision collision)
{
if (collision.collider.tag == "User")
{
renderer.sharedMaterial = Material[1];
}
else
{
renderer.sharedMaterial = Material[2];
}
}
}
}
How can I change the colour of an object I created and transferred into Unity when it collides with a user in a VR world??? (Using C#). My code has to include a loop, an "if" statement, a function and user interaction. PLEASE HELP!!!!! ,using System.Collections; using System.Collections.Generic; using UnityEngine;
public class SilverFernScript : MonoBehaviour {
void Start()
{
Renderer = GetComponet<Renderer>();
private void OnPreRender()
{
renderer.sharedMaterial = Material[0];
}
private void OnCollisionEnter(Collision collision)
{
if (collision.collider.tag == "User")
{
renderer.sharedMaterial = Material[1];
}
else
{
renderer.sharedMaterial = Material[2];
}
}
}
}
Answer by UnityCoach · Sep 25, 2018 at 11:35 AM
There are more than one question in your question.
Assuming you have a MeshRenderer on that object, you want to change the colour of that Renderer's Material, if it has a colour property.
using UnityEngine;
public class SilverFernScript : MonoBehaviour
{
[SerializeField] Material[] materials = new Material[2];
Renderer _renderer;
private void Awake ()
{
_renderer = GetComponent<Renderer>();
_renderer.sharedMaterial = materials[0];
}
private void OnCollisionEnter(Collision collision)
{
if ( collision.collider.CompareTag("User") )
_renderer.sharedMaterial = materials[1];
}
}
You also need to add a Collider on the Camera (or anything linked to your VR player), and tag it as "User". You also need to add a Rigidbody component to at least one of the two, and set it as "Is Kinematic" so that it simply doesn't suffer Physics.