Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

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

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


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