- Home /
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
}
}
}
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.
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.
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.
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.
Your answer
Follow this Question
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