• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Johane · Feb 14, 2016 at 07:10 PM · c#howoptimisationsimplepath

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); 
         }
     }
 
 
 
 }


Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image EmHuynh · Feb 14, 2016 at 08:30 PM 0
Share

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 );
         }
 
     }
 }

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
     }
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Johane · Feb 14, 2016 at 10:05 PM

Thanks to both @EmHuynh and @Ahndrakhul! Really helpful advise, much obliged!

J

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image EmHuynh · Feb 15, 2016 at 01:36 AM 0
Share

Glad we could help!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

97 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Changing sprites of a 2D object in C#, Can't figure it out! 1 Answer

Identical Lines of Code Will Either Disable, or Wont Disable a Script. Need Help. 0 Answers

How to make ball launch in direction of line when dragged back and released?,Drag your mosue back and a line points at where youre aiming and upon letting go it slingshots in that direction 0 Answers

How best to make an infinite ocean? 0 Answers

2D Arrays Help. Why to use 2D arrays for 2 ints 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges