Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by davidflynn2 · Aug 13, 2013 at 07:06 PM · c#arrayconvert

Using an array instead

I have the following code and I am wondering if there is any way I can convert this over to use and array to clean I up some. What I need is an array for the tags and an array of damage and some how make them correspond and remove the health via that. I am not quite for sure if this is possible or not. Any on one please lend me a hand?

     private int shipHealthMax = 100;// The ships Max health
     public int curShipHealth = 100; // The ships current health
 public int Missiile = 0;//Allows you to set the damage from this in the insepctor.
     public int Enemy = 0;//Allows you to set the damage from this in the insepctor.
     public int Metoeor = 0;//Allows you to set the damage from this in the insepctor.
     public int AMothership = 0;//Allows you to set the damage from this in the insepctor.
     public int EMothership = 0;//Allows you to set the damage from this in the insepctor.
     public int Alliance = 0;//Allows you to set the damage from this in the insepctor.
     public int ESatellite = 0;//Allows you to set the damage from this in the insepctor.
     public int ASatellite = 0;//Allows you to set the damage from this in the insepctor.
     public int APlanet = 0;//Allows you to set the damage from this in the insepctor.
     public int EPlanit = 0;//Allows you to set the damage from this in the insepctor.
 
 
 
 void OnCollisionEnter(Collision collision) //When collided
     {
         if(shield.activeSelf == false)// Checks to make sure the shield is not active
         {
             if (collision.gameObject.tag == "Missile")//checks the tag of what we collided with it if is a missle.
                 {
                     curShipHealth -= Missiile;//It would apply this dammage
                 }
              if (collision.gameObject.tag == "Enemy")//Again checks what we collided with if it is a enemy
                 {
                     curShipHealth -= Enemy;//It would apply this dammage
                 }
     
              if (collision.gameObject.tag == "Meteor")//Again checks what we collided with if it is a Meteor
                 {
                     curShipHealth -= Metoeor;//It would apply this dammage
                 }
     
               if (collision.gameObject.tag == "AllianceMothership")//Again checks what we collided with if it is a Alliance mothership
                 {
                     curShipHealth -= AMothership;//It would apply this dammage
                 }
              if (collision.gameObject.tag == "Alliance")//Again checks what we collided with if it is a Allance
                 {
                     curShipHealth -= Alliance;//It would apply this dammage
                 }
              if (collision.gameObject.tag == "EnemyMothership")//Again checks what we collided with if it is a EnemyMothership
                 {
                     curShipHealth -= EMothership;//It would apply this dammage
                 }
              if (collision.gameObject.tag == "EnemySatellite")//Again checks what we collided with if it is a EnemySatellite
                 {
                     curShipHealth -= ESatellite;//It would apply this dammage
                 }
              if (collision.gameObject.tag == "AllianceSatellite")//Again checks what we collided with if it is a AllianceSatellite
                 {
                     curShipHealth -= ASatellite;//It would apply this dammage
                 }
              if (collision.gameObject.tag == "AlliancePlanet")//Again checks what we collided with if it is a AlliancePlanet
                 {
                     curShipHealth -= APlanet;//It would apply this dammage
                 }
              if (collision.gameObject.tag == "EnemyPlanet")//Again checks what we collided with if it is a EnemyPlanet
                 {
                 curShipHealth -= EPlanit;//It would apply this dammage
             }
 
         }
         
     }
 
 
 
 
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by flaviusxvii · Aug 13, 2013 at 07:18 PM

I might suggest a fundamentally different approach. I'll let you figure some of it out, but imagine the possibilities if your OnCollisionEnter function looked like this:

 void OnCollisionEnter(Collision collision)
 {
     if(!shield.activeSelf) {
         DamageInflictor di = collision.gameObject.GetComponent<DamageInflictor>();
             if(di) {
                 curShipHealth -= di.damage;
             }
     }
 }

So you'd write DamageInflictor and put it on every type of thing that can cause damage.

Comment
Add comment · Show 4 · 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 Joyrider · Aug 13, 2013 at 07:21 PM 0
Share

This might indeed be a more optimized solution.

avatar image davidflynn2 · Aug 13, 2013 at 07:29 PM 0
Share

Ok so basically use that in the collision and each game object needs to have a script that stores a value of the damage it does on it. Thanks make great since not sure why did not think of that thanks so much.

avatar image flaviusxvii · Aug 13, 2013 at 07:31 PM 1
Share

I hope it works out.

avatar image Jamora · Aug 13, 2013 at 07:34 PM 1
Share

This solution is called the Command Pattern. Read further on Design patterns when you have a little time to spare and want to get ideas on program$$anonymous$$g.

avatar image
1

Answer by Joyrider · Aug 13, 2013 at 07:18 PM

You could use a switch statement but that would only shorten it a little.

Otherwise you can use a structure, an array and a for-loop, like this:

 public struct stDamages
 {
     public string tag;
     public int damage;
 }
 
 public stDamages[] damagePointArray;
 public stDamages damagePoint;
 
 void OnCollisionEnter(Collision collision)
 {
     if(shield.activeSelf == false)// Checks to make sure the shield is not active
     {
         for(int i=0; i<damagePointArray.Length;i++)
         {
             damagePoint= damagePointArray[i];
             if(collision.gameObject.tag == damagePoint.tag)
                 curShipHealth -= damagePoint.damage
         }
     }
 }

All you have to do is fill the array in the inspector.

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

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

18 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

Related Questions

Multiple Cars not working 1 Answer

Convert to array. 1 Answer

Getting ArgumentOutOfRangeException on array c# 1 Answer

Get JSON array object string value 2 Answers

Converting a string to an int 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