- Home /
C# how to change material's color using if
using UnityEngine;
using System.Collections;
public class Block : MonoBehaviour {
public type Type = Type.Red;
void Start(){
MeshRenderer mr = GetComponent();
mr.material = Resources.Load("Materials/"+Type.ToString()) as Material;
}
void OnCollisionEnter(Collision col){
Score.score += (int)Type;
if (Type.Red){
Destroy(gameObject);
}
if (Type.Green){
renderer.material.color = Color.red;
}
if (Type.Blue){
renderer.material.color = Color.green;
}
Destroy(gameObject);
}
public enum Type{
Red = 10,
Green = 15,
Blue = 20
} }
if (Type.Red){
Destroy(gameObject);
}
if (Type.Green){
renderer.material.color = Color.red;
}
if (Type.Blue){
renderer.material.color = Color.green;
if's don't work Error: error CS0029: Cannot implicitly convert type Type' to
bool'
How do I make it work?
Answer by sven1994 · Nov 05, 2011 at 11:50 PM
Wrong:
if(Type.Blue)
True:
if(type == Type.Blue)
You might also want to use "else if"s or even better:
switch(type)
{
case Type.Red:
Destroy(gameObject);
break;
case Type.Green:
renderer.material.color = Color.red;
break;
case Type.Blue:
renderer.material.color = Color.green;
break;
}
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
A node in a childnode? 1 Answer
How to apply a shader to all materials inside a gameobject 1 Answer
C# - How to smoothly & slightly move camera while player moves? 1 Answer