BoxCollider2D not working after enabling and disabling. Also Player Tracking not working.

I have a problem where I enable the collider when I do an attack and it doesn’t detect collisions. It detects when it starts enabled only. I know that I could store a boolean that says if you are attacking and that is checked when a collision occurs, but it is more optimized to only use the colliders sometimes and I don’t know why it doesn’t work.

Script that checks collisions:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class dmg : MonoBehaviour
{
    [SerializeField] float dmgv;
    [SerializeField] GameObject MyPlayer;
    void OnTriggerEnter2D(Collider2D col){
        if(col.gameObject.GetComponent<Player>() != null && col.gameObject != MyPlayer){
            col.gameObject.GetComponent<Player>().Knockback(dmgv, transform.localScale.x/Mathf.Abs(transform.localScale.x), MyPlayer.GetComponent<Player>().ducked);
            Debug.Log("Hit");
        }
    }
}

Script that enables and disables it:

public void HitboxStart(){
        hitboxes[attack].enabled = true;
    }
    public void HitboxEnd(){
        hitboxes[attack].enabled = false;
    }

My other problem is that I made a script to track the players and zoom in and out, but when the players get too close it suddenly zooms out.

Here is the script I put on the players:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Target : MonoBehaviour
{
    TargetTracker TargetTracker;
    // Start is called before the first frame update
    void Start()
    {
        TargetTracker = FindObjectOfType<TargetTracker>();
    }

    // Update is called once per frame
    void Update()
    {
        if(transform.position.x > TargetTracker.right.transform.position.x){
            TargetTracker.right = gameObject;
        }else if(transform.position.x < TargetTracker.left.transform.position.x){
            TargetTracker.left = gameObject;
        }else if(transform.position.y > TargetTracker.up.transform.position.y){
            TargetTracker.up = gameObject;
        }else if(transform.position.y < TargetTracker.down.transform.position.y){
            TargetTracker.down = gameObject;
        }
    }
}

Here is the script I put on my camera:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TargetTracker : MonoBehaviour
{
    public GameObject left;
    public GameObject right;
    public GameObject down;
    public GameObject up;
    public float boundleft;
    public float boundright;
    public float bounddown;
    public float boundup;
    [SerializeField] float padding;
    Camera camera;
    // Start is called before the first frame update
    void Start()
    {
        camera = GetComponent<Camera>();
    }

    // Update is called once per frame
    void Update()
    {
        float upf = up.transform.position.y;
        float downf = down.transform.position.y;
        float leftf = left.transform.position.x;
        float rightf = right.transform.position.x;
        if(upf>boundup){
            upf=boundup;
        }
        if(downf<bounddown){
            downf=bounddown;
        }
        if(leftf<boundleft){
            leftf=boundleft;
        }
        if(rightf>boundright){
            rightf=boundright;
        }
        if(rightf-leftf>upf-downf){
            camera.orthographicSize = Screen.height/((Screen.width/(rightf-leftf))*2)+padding;
        }else{
            camera.orthographicSize = (upf-downf)/2+padding;
        }
        transform.position = new Vector3(leftf+((rightf-leftf)/2),downf+((upf-downf)/2),-10);
    }
}

I see your TargetTracker driving its position directly through transform.position. Does that have either collider on it? If so, you are bypassing physics because you are not calling .MovePosition() on the rigidbody on the moving object.

How do you arrive at this? Have you profiled? It may actually be more computationally expensive because colliders are not only removed from scene but an extra trip has to go to the physics engine and tell them to stop considering it, then start reconsidering it again when you enable.

Anyway, the point is, you can’t guess about this stuff. DO NOT OPTIMIZE CODE JUST BECAUSE… If you don’t have a problem, DO NOT OPTIMIZE!

If you DO have a problem, always start by using the profiler:

Window → Analysis → Profiler

Failure to use the profiler means you’re just guessing, making a mess of your code for no good reason.

https://discussions.unity.com/t/841163/2

Notes on optimizing UnityEngine.UI setups:

https://discussions.unity.com/t/846847/2

1 Like

Yeah, I doubt this is anything related to physics. There’s so much code above I don’t even want to begin figuring it out.

If you want to make a statement about physics being essentially broken in that case, I’d say spin up an empty project and do a simple test to verify your assumption. When it works there, you know your code is likely doing something funky. These kinds of tests are so quick to do.

Also, FYI: each collider type isn’t special for such generic things so saying a BoxCollider2D doesn’t work when enabled from script is actually saying no collider works when enabled from script. Each collider does not have its own special code for that.

1 Like

It does not have a collider on it. I think the issue is to do with the padding applied or something, or a rounding thing.

I decided to not try disabling the collider but instead using a boolean variable to say if the collider should be checked. For some reason it still doesn’t work. I am using OnTriggerStay and I checked to see the boolean and it was true while the hit box overlapped the other player. Initiating the variable as true makes it work.