- Home /
 
How do I have a game object access the enums on another game object on collision?
Hi, I just have a quick question.
So I'm making a 2D game and to make it easier on myself in the long-run I wanted to create a system where I input the various properties of an attack and when my player character is hit it will react according to what I've put as it's responses.
My problem is that I don't exactly know how to access the properties of an object while it is colliding in the code. Nor do I know if that it is actually the best way to go about it.
Firstly, I want to be able to differentiate between a light and a heavy attack so that a different amount of health can be taken from the player character, and if it was just two different attack types I would used scripts but I want there to be several different parameters that the player can react to.
Here is what my Enemy Attack Parameter Script looks like: using System.Collections; using System.Collections.Generic; using UnityEngine;
 public enum EnemyAttackStrength
 {
     light,
     heavy,
 }
  
  
 public enum EnemyAttackType
 {
     melee,
     ranged,
     special,
 }
  
 public enum EnemyAttackElement
 {
     none,
     fire,
     ice,
     electric,
     crystal,
     poision,
 }
  
 public class EnemyAttack: MonoBehaviour
 {
     public EnemyAttackStrength thisAttackStrength;
     public EnemyAttackType thisAttackType;
     public EnemyAttackElement thisAttackElement;
 }
 
               And here is the excerpt where I'm trying to have the player detect what enum the enemy attack is:
     private void OnTriggerEnter2D(Collider2D collision)
         {
             if (collision.gameObject.tag == ("Enemy"))
             {
                 if (collision.gameObject.GetComponent<EnemyAttack>()) //obviously a mockup
                 {
                     if (thisPlayerInvulnerability == playerInvulnerability.vulnerable)
                     {
                         health = health - 5;
                         player.tookDamage = true;
                         ChangeVulnerability(playerInvulnerability.invulerable);
                         StartCoroutine(InvincibilityTimer());
                     }
                     else if (thisPlayerInvulnerability == playerInvulnerability.invulerable)
                     {
                         health = health - 0;
                         player.tookDamage = false;
                     }
                 }
             }
 
               Thank you for anyone willing to help, I really appreciate it!
Answer by FZ_Applications · May 11, 2020 at 09:00 PM
Here is an example how to enter the enenies enums:
 private void OnTriggerEnter2D(Collider2D collision) {
        if (collision.CompareTag("Enemy")) {
            EnemyAttack enemyAttack = collision.GetComponent<EnemyAttack>()
            if (enemyAttack != null) {
                 EnemyAttackStrength enemyAttackStrength = enemyAttack.thisAttackStrenght
                 //...
            }
       }
 }
 
              Your answer
 
             Follow this Question
Related Questions
Allow 2D character overlaps in side-scroller? 1 Answer
Creating interactive slime touch game unity 0 Answers
Using IgnoreLayerCollision and IgnoreCollision together 0 Answers
Collider Is Touching Layers is not working... 1 Answer
Beginner trying to stop movement function and collision on death ? 0 Answers