#OnTriggerEnter2D isn't working

1 messages · Page 1 of 1 (latest)

long goblet
#

I am trying to make a combination of scripts that causes the character to touch a laser beam, then the player loses 15% health. I was using the OnTriggerEnter2D function but it wasn't working. I will list the scripts below.

BeamScript.cs

using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class BeamScript : MonoBehaviour
{
    public LogicScript LogicManager;
    
    // Start is called before the first frame update
    void Start(){
        LogicManager = GameObject.FindGameObjectWithTag("LogicManager").GetComponent<LogicScript>();
    }

    private void OnTriggerEnter2D(Collider2D collider)
    {
        Debug.Log("Collision Found");
        if (collider.gameObject.layer == 6)
        {
            LogicManager.DecreaseHealth(15);
        }
    }
}```

`LogicScript.cs`
```cs
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor.Rendering;
using UnityEngine;
using UnityEngine.UI;

public class LogicScript : MonoBehaviour{

    public Double Score;
    public Text ScoreText;
    public Double ScoreSpeedMultiplier;

    public Double Health;
    public Text HealthText;
    
    // Start is called before the first frame update
    void Start(){
        
    }

    // Update is called once per frame
    void Update(){
        IncreaseScore(1);
    }

    
    public void IncreaseScore(int IncrementAmount){
        Score += IncrementAmount * Time.deltaTime * ScoreSpeedMultiplier;
        ScoreText.text = Math.Floor(Score).ToString();
    }
    
    public void DecreaseHealth(int DecrementAmount)
    {
        Health -= DecrementAmount;
        HealthText.text = Health.ToString();
    }
}

If I need to add any imports to either of the files or any other changes, please let me know. I know that the OnTriggerEnter2D is the problem because even the Debug.Log wouldn't show up.

#

And the Beam is set to a trigger so that also isn't the problem.

ashen gale
long goblet
long goblet
#

I've tried that before and it didn't change anything

ashen gale
#

keep going through all of the steps

ashen gale
long goblet
#

Most of what this is I don't really understand as I am a new developer.

#

Is anyone else able to figure out what the problem is

ashen gale
#

but i can pretty much guarantee that the answer to your issue is in those steps i linked

long goblet
#

Fixed, I kinda forgot to add the script to the game object...