How do I code this in a simpler way?
Hello,
So the below code works fine but I can't help thinking there must be a more efficient way of doing this. The frequency of the colour change doesn't matter atm, I just want to be able to sync it somehow with the collisions.
Many Thanks
J
using UnityEngine; using System.Collections;
public class DestroyedByBall : MonoBehaviour
{
//int count; .
public int countStart; //everytime another collider hits this start value reduces by 1/
void Start()
{
countStart =30;
}
void OnTriggerEnter (Collider other)
{
if (other.CompareTag ("Player")) {
countStart-- ; //everytime another collider hits this goes up one
}
if (countStart == 20)
{
gameObject.transform.parent.GetComponent<Renderer> ().material.color = Color.black;
}
if (countStart == 10)
{
gameObject.transform.parent.GetComponent<Renderer> ().material.color = Color.yellow;
}
if (countStart == 5)
{
gameObject.transform.parent.GetComponent<Renderer> ().material.color = Color.red;
}
if (countStart == 0)
{
//destroys parent of gameoject ie the whole gameobject
Destroy (transform.parent.gameObject);
}
}
}
Hey, @Johane. I have a suggestion. Your source code will be cleaner if you use switch statement to deal with the variable countStart
. It also makes it easier to read and understand.
Like so:
using UnityEngine;
using System.Collections;
public class DestroyedByBall : $$anonymous$$onoBehaviour {
public int startCount;
Color parent$$anonymous$$aterialColor;
void Start() {
startCount = 30;
}
void OnTriggerEnter( Collider other )
{
if( other.CompareTag( "Player" ) ) {
startCount --;
}
if( startCount > 0 )
{
switch( startCount ) {
case 20:
parent$$anonymous$$aterialColor = Color.black;
break;
case 10:
parent$$anonymous$$aterialColor = Color.yellow;
break;
case 5:
parent$$anonymous$$aterialColor = Color.red;
break;
}
gameObject.transform.parent.GetComponent< Renderer >().material.color = parent$$anonymous$$aterialColor;
}
else {
Destroy( transform.parent.gameObject );
}
}
}
Answer by Ahndrakhul · Feb 14, 2016 at 08:46 PM
EDIT: Looks like someone beat me to it!
You could do something like this:
using UnityEngine;
public class DestroyedByBall : MonoBehaviour
{
public int countStart = 30;
Material material;
void Start()
{
material = transform.parent.GetComponent<Renderer>().material;
}
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
countStart--;
HandleCountdown();
}
}
void HandleCountdown()
{
switch (countStart)
{
case 20:
ChangeMaterialColor(Color.black);
break;
case 10:
ChangeMaterialColor(Color.yellow);
break;
case 5:
ChangeMaterialColor(Color.red);
break;
case 0:
Destroy(transform.parent.gameObject);
break;
default:
break;
}
}
void ChangeMaterialColor(Color matColor)
{
material.color = matColor;
}
}
Answer by Johane · Feb 14, 2016 at 10:05 PM
Thanks to both @EmHuynh and @Ahndrakhul! Really helpful advise, much obliged!
J
Your answer
Follow this Question
Related Questions
Issue with loading scenes with buttons 1 Answer
Changing order in layers at runtime with a coroutine 0 Answers
An update that behaves like a singleton in a monobehavior 2 Answers
Expanding onto the 2D RougeLike Game adding a main menu (URGENT! help required please) 1 Answer
Changing sprites of a 2D object in C#, Can't figure it out! 1 Answer