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. Dismiss Notice

Resolved OnTriggerEnter2D was working fine yesterday, but today nothing is happening.

Discussion in 'Scripting' started by Nomad-CA, Sep 6, 2023.

  1. Nomad-CA

    Nomad-CA

    Joined:
    Feb 7, 2022
    Posts:
    9
    Hello,

    Yesterday I was working on making chests spawn random items when the player collided with them. It was working fine. This morning I realized the trigger is no longer firing. The player walks right through the chest and nothing happens. The chests work if I implement a GetKeyDown on Update but no longer with OnTriggerEnter2D. I am very confused on why it decided to stop working today. Has anyone ever run into an issue similar to this?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5. using TinyScript;
    6.  
    7. public class Spawner : MonoBehaviour
    8. {
    9.     public LootDrop Loot;
    10.     public int RandomDropCount = 1; // should be adjusted to drop equal to player count
    11.     public float DropRange = .5f;
    12.     private Animator animator;
    13.  
    14.     // Use this for initialization
    15.     void Start()
    16.     {
    17.         animator = GetComponent<Animator>();
    18.     }
    19.    
    20.     private void OnTriggerEnter2D(Collider2D collision)
    21.     {
    22.         if (collision.gameObject.GetComponent<Chest>())
    23.         {
    24.             Debug.Log("Chest Contact");
    25.             animator.SetBool("opened", true);
    26.             //animator.SetBool(Settings.use, true);
    27.             Loot.SpawnDrop(this.transform, RandomDropCount, DropRange);
    28.  
    29.             gameObject.GetComponent<BoxCollider2D>().isTrigger = false;
    30.  
    31.             // if "opened" is set to true
    32.             // do not spawn anymore items
    33.             Destroy(this);
    34.         }
    35.     }
    36.  
    37.     /*
    38.     public void EnemyDropItem()
    39.     {
    40.         Loot.SpawnDrop(this.transform, RandomDropCount, DropRange);
    41.     }*/
    42.  
    43.     /*
    44.     void Update()
    45.     {
    46.         if (Input.GetKeyDown(KeyCode.E))
    47.         {
    48.             Debug.Log("Chest Contact");
    49.             animator.SetBool("opened", true);
    50.             //animator.SetBool(Settings.use, true);
    51.             Loot.SpawnDrop(this.transform, RandomDropCount, DropRange);
    52.  
    53.             gameObject.GetComponent<BoxCollider2D>().isTrigger = false;
    54.  
    55.             // if "opened" is set to true
    56.             // do not spawn anymore items
    57.             Destroy(this);
    58.         }
    59.     }*/
    60. }
    61.  
     

    Attached Files:

  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,718
    From your screenshot your Chest object doesn't seem to have the Chest component attached to it.
     
  3. Nomad-CA

    Nomad-CA

    Joined:
    Feb 7, 2022
    Posts:
    9
    Its on there. Here is a screen shot will all the components.
     

    Attached Files:

  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,718
    Wait Spawner is on the Chest? Based on the code it should be on the player. It's expecting the OTHER object to have a Chest component on it.
     
    Nomad-CA likes this.
  5. Nomad-CA

    Nomad-CA

    Joined:
    Feb 7, 2022
    Posts:
    9
    Ok, I edited the code. This fixed the issue with the chests. I also want the enemy's to drop items from a different loot table so an issue come up when the Player collides with enemies but for now the chest issue is fixed. Thanks!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5. using TinyScript;
    6.  
    7. public class Spawner : MonoBehaviour
    8. {
    9.     public LootDrop Loot;
    10.     public int RandomDropCount = 1; // should be adjusted to drop equal to player count
    11.     public float DropRange = .5f;
    12.     private Animator animator;
    13.  
    14.     // Use this for initialization
    15.     void Start()
    16.     {
    17.         animator = GetComponent<Animator>();
    18.     }
    19.    
    20.     private void OnTriggerEnter2D(Collider2D collision)
    21.     {
    22.         if (collision.gameObject.GetComponent<Player>())
    23.         {
    24.             Debug.Log("Chest Contact");
    25.             animator.SetBool("opened", true);
    26.             //animator.SetBool(Settings.use, true);
    27.             Loot.SpawnDrop(this.transform, RandomDropCount, DropRange);
    28.  
    29.             gameObject.GetComponent<BoxCollider2D>().isTrigger = false;
    30.  
    31.             // if "opened" is set to true
    32.             // do not spawn anymore items
    33.             Destroy(this);
    34.         }
    35.     }
    36.  
    37.    
    38.     public void EnemyDropItem()
    39.     {
    40.         Loot.SpawnDrop(this.transform, RandomDropCount, DropRange);
    41.     }
    42. }
    43.