Search Unity

The best way to detected Collisions and GetComponent

Discussion in 'Getting Started' started by darthachill, Mar 2, 2015.

  1. darthachill

    darthachill

    Joined:
    Jan 25, 2015
    Posts:
    27
    Problem solved! I changed:
    Code (CSharp):
    1.  TakingDamage(other.GetComponent<BaseSpell>().Dmg); // It's Working!
    But if you have some advice, i will greateful!
    ======================================================
    I have GameObject: SmallIceBolt, and i Created and Added Script SmallIceBolt to this GameObject. The same names help me later.

    SmallIceBolt inherited from BaseSpell.
    Code (CSharp):
    1. public class BaseSpell : MonoBehaviour
    2. {
    3.     public int SpellName { get; set; }
    4.     public int Dmg { get; set; }
    5. }
    Code (CSharp):
    1. public class SmallIceBolt : BaseSpell
    2. {
    3.     public SmallIceBolt()
    4.     {
    5.         SpellName = "SmallIceBolt";
    6.         Dmg = 10;
    7.     }
    8. }
    For Example I Have many different Spells and GameObjectes with the same names Scripts( say 10 000, everyone Inherited from BaseSpell), and I want to write one function which help me detected what kind of Spell it is. All of Spells GameObject I Taged "Damage". I thought that name Scrpit like GameObject, was good idea, and it Will help me to define which Component I need to get. But it's not working.
    How Can I GetComponent<FireBall> from GameObject name "FireBall" and GetComponent<SmallIceBolt> from GameObject name "SmallIceBolt", can I do it in One function with only one condition if ?


    Code (CSharp):
    1. public class EnemyWizard1 : BaseEnemy
    2. {
    3.     void OnTriggerEnter(Collider other)
    4.     {
    5.         if (other.tag.Equals("Damage"))
    6.             TakingDamage(other.GetComponent<other.name>().Dmg); // Not Working !
    7.     }
    8.  
    9.     public void TakingDamage(int Dmg)
    10.     {
    11.         HpLeft -= Dmg;
    12.     }
    13. }
     
    Last edited: Mar 2, 2015
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    You hinted to the problem in your example: While it's unlikely that you'll have 10,000 different types of spells, any amount over 25 (to me, anyway) is too many different types of classes for something like this.

    Why does each spell have to be its own class? Why not one class ("Spell") that has properties that indicate what type it is, how much damage it does, its mana cost, etc.? Or at the most separate spells out by type so you can keep similar functionality together (Heal vs Damage (although creatively, healing could just be negative damage)).

    Look into the GameObject.SendMessage() method. The spell class (or its inherited classes) could each contain a method called DoEffect() that performs any of its necessary actions. Then, something like:
    Code (CSharp):
    1. void OnTriggerEnter(Collider other) {
    2.   if (other.tag.Equals("Spell") {
    3.     other.SendMessage("DoEffect");
    4.   }
    5. }
    should do the trick.