- Home /
Collision issue
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class WhiteSheep : MonoBehaviour { Renderer _renderer; //Declaring _renderer public GameObject[] trees; //Declaring trees for OnCollisionEnter
void Awake()
{
_renderer = GetComponent<Renderer>();
//This is to not repeat GetComponent<Renderer>();
}
IEnumerator Start()
//This is so that I can use a loop in a Start().
{
for (int i = 0; i < 40; i++)
//This will make it repeat changing its colour for 40 times.
{
int colour = getRandomInt(0, 3);
//There are three different colours that it can change into.
if (colour == 1)
{
Renderer rend = GetComponent<Renderer>();
//so we can access the colour
rend.material.shader = Shader.Find("_Color");
rend.material.SetColor("_Color", Color.black);
rend.material.shader = Shader.Find("Specular");
rend.material.SetColor("_SpecColor", Color.black);
//Find the Specular shader and change its Color to black
yield return null;
//This is for Coroutines so it loops.
}
else if (colour == 0)
{
Renderer rend = GetComponent<Renderer>();
//so we can access the colour
rend.material.shader = Shader.Find("_Color");
rend.material.SetColor("_Color", Color.gray);
rend.material.shader = Shader.Find("Specular");
rend.material.SetColor("_SpecColor", Color.gray);
//Find the Specular shader and change its Color to gray
yield return null;
//This is for Coroutines so it loops.
}
else if (colour == 2)
{
Renderer rend = GetComponent<Renderer>();
//so we can access the colour
rend.material.shader = Shader.Find("_Color");
rend.material.SetColor("_Color", Color.red);
rend.material.shader = Shader.Find("Specular");
rend.material.SetColor("_SpecColor", Color.red);
//Find the Specular shader and change its Color to red
yield return null;
//This is for Coroutines so it loops.
}
}
}
private int getRandomInt(int start, int end)
//This is to make getRandomInt as a function
{
float fRandom = Random.Range(start, end);
int iRandom = Mathf.FloorToInt(fRandom);
return iRandom;
}
private void OnCollisionEnter(Collision collision)
{
if (collision.collider.CompareTag ("Player"))
{
foreach (GameObject tree in trees)
//Foreach loop is to block a statement for each element
{
int colour = getRandomInt(0, 2);
if (colour == 1)
{
Renderer rend = tree.GetComponent<Renderer>();
rend.material.shader = Shader.Find("_Color");
rend.material.SetColor("_Color", Color.yellow);
rend.material.shader = Shader.Find("Specular");
rend.material.SetColor("_SpecColor", Color.yellow);
//Find the Specular shader and change its Color to yellow
}
if (colour == 0)
{
Renderer rend = tree.GetComponent<Renderer>();
rend.material.shader = Shader.Find("_Color");
rend.material.SetColor("_Color", Color.red);
rend.material.shader = Shader.Find("Specular");
rend.material.SetColor("_SpecColor", Color.red);
//Find the Specular shader and change its Color to red
}
}
}
}
}
I want to make a tree to change its colour when the player collides into the WhiteSheep. The colour of the sheep changes perfectly, but when the player collides with the sheep, tree colour doesn't change. But no errors appear in Unity... Can you please help me?
Answer by GraemeJudkins · Sep 27, 2018 at 12:03 AM
You could try changing the "_emissionColor" in the material, open up the material inspector during runtime and see if changing any of the material properties changes the colour of the tree in unity. You will need to open the debug inspector to do so The material will display extra options, at the bottom will be material colours