Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question How to I access (or read) another game object's enums through OnTrigger collisions?

Discussion in '2D' started by noahl_unity, May 9, 2020.

  1. noahl_unity

    noahl_unity

    Joined:
    Feb 17, 2020
    Posts:
    5
    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:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public enum EnemyAttackStrength
    6. {
    7.     light,
    8.     heavy,
    9. }
    10.  
    11.  
    12. public enum EnemyAttackType
    13. {
    14.     melee,
    15.     ranged,
    16.     special,
    17. }
    18.  
    19. public enum EnemyAttackElement
    20. {
    21.     none,
    22.     fire,
    23.     ice,
    24.     electric,
    25.     crystal,
    26.     poision,
    27. }
    28.  
    29. public class EnemyAttack: MonoBehaviour
    30. {
    31.     public EnemyAttackStrength thisAttackStrength;
    32.     public EnemyAttackType thisAttackType;
    33.     public EnemyAttackElement thisAttackElement;
    34. }
    And here is the excerpt where I'm trying to have the player detect what enum the enemy attack is:
    Code (CSharp):
    1.  private void OnTriggerEnter2D(Collider2D collision)
    2.     {
    3.         if (collision.gameObject.tag == ("Enemy"))
    4.         {
    5.             if (collision.gameObject.GetComponent<EnemyAttack>()) //obviously a mockup
    6.             {
    7.                 if (thisPlayerInvulnerability == playerInvulnerability.vulnerable)
    8.                 {
    9.                     health = health - 5;
    10.                     player.tookDamage = true;
    11.                     ChangeVulnerability(playerInvulnerability.invulerable);
    12.                     StartCoroutine(InvincibilityTimer());
    13.                 }
    14.                 else if (thisPlayerInvulnerability == playerInvulnerability.invulerable)
    15.                 {
    16.                     health = health - 0;
    17.                     player.tookDamage = false;
    18.                 }
    19.             }
    20.         }
    Thank you for anyone willing to help, I really appreciate it!
     
  2. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    Just assign the collision.gameObject.GetComponent<EnemyAttack>() to an EnemyAttack local variable. Then you access it via that variable.

    Just for example, here is my code in my game for a playercontroller when the player activates a grab:

    Code (CSharp):
    1. public void Grab(bool grabAll = false)
    2.     {
    3.         Collider2D[] potentialPickups = Physics2D.OverlapCircleAll(GrabPosition.position, GrabRadius, GrabLayers);
    4.         if (potentialPickups != null && potentialPickups.Length > 0)
    5.         {
    6.             Collider2D firstPotentialPickup = potentialPickups[0];
    7.             ResourceNodeController rnc = firstPotentialPickup.GetComponent<ResourceNodeController>();
    8.             if (rnc != null)
    9.             {
    10.                 ResourceNode rn = GameCore.Current.Resources[rnc.ResourceNodeID];
    11.                 if (rn.HarvestHealth == 0)
    12.                 {
    13.                     InventoryContainer results = rnc.GrabResource();
    14.                     if (results != null)
    15.                     {
    16.                         PlayerData.Current.Inventory.MergeContainers(results);
    17.                         if (!results.IsEmpty())
    18.                             PlayerData.Current.ToolBar.MergeContainers(results);
    19.                         if (!results.IsEmpty())
    20.                         {
    21.                             GameObject go = Instantiate(GameResources.Current.LootBagPrefab, transform.position, Quaternion.identity);
    22.                             InventoryContainerController lc = go.GetComponent<InventoryContainerController>();
    23.                             lc.InventoryContainer_var = results;
    24.                             lc.Initialize();
    25.                         }
    26.                     }
    27.                 }
    28.             }
    29.             else
    30.             {
    31.                 IInteractableElement iie = firstPotentialPickup.GetComponent<IInteractableElement>();
    32.                 //InventoryContainerController lc = firstPotentialPickup.GetComponent<InventoryContainerController>();
    33.                 if (iie != null)
    34.                 {
    35.                     if (grabAll && iie is InventoryContainerController icc)
    36.                     {
    37.                         PlayerData.Current.Inventory.MergeContainers(icc.InventoryContainer_var);
    38.                         if (!icc.InventoryContainer_var.IsEmpty())
    39.                             PlayerData.Current.ToolBar.MergeContainers(icc.InventoryContainer_var);
    40.                     }
    41.                     iie.DisplayUI();
    42.                 }
    43.             }
    44.         }
    45.     }
     
  3. noahl_unity

    noahl_unity

    Joined:
    Feb 17, 2020
    Posts:
    5
    My god you did it... Thank you so much