Cant get a collider to enable through another script, please help

I’m making a 2d Platformer and when the sword is picked up the hasCollided Boolean has to be set to true and L has to be pressed, now im making an empty game object with a seperate script parented to the player containing a collided, which when the L key is pressed and the hasCollided = true it will enable, if not it will disable the collider. however despite making the variables from the sword script public the hasCollided in the empty game object script isn’t recognised, can someone help please?
and this is the code for the object containing the collider i want to pass it to

This is the sword script which contains hasCollided and if L is pressed condition

using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
  
    public class powerup : MonoBehaviour
    {
        // Start is called before the first frame update
        public GameObject player;
        private bool hasCollided = false;
        public Animator anim;
        public bool attacking = false;
  
        private void Awake()
        {
            hasCollided = false;
            attacking = false;
        }
  
        void Update()
        {
            if (hasCollided && Input.GetKeyDown(KeyCode.Mouse0))
            {
                player.GetComponent<Animator>().SetBool("coll", true);
                anim.SetTrigger("Punch");
                //hasCollided = false;
                attacking = true;
              
            }
        }
  
        private void OnTriggerEnter2D(Collider2D other)
        {
            if (other.gameObject.name == "Player")
            {
                hasCollided = true;
                attacking = true;
              
                gameObject.GetComponent<SpriteRenderer>().enabled = false;
  
            }
        }
    }

and this is the code for the object containing the collider i want to pass it to

    public class attackscript : MonoBehaviour
    {
        // Start is called before the first frame update
        public GameObject Player;
        public GameObject sword;
        public powerup script;
        public BoxCollider2D attackcol;
        private Animator anim;
        public GameObject collidescript; 
   
        void Awake()
        {
           
            attackcol.enabled = false;
         
           
        }
   
        // Update is called once per frame
        void Update()
        {
            if (hasCollided && Input.GetKeyDown(KeyCode.Mouse0))
            {
   
                attackcol.enabled = true;
   
            }
   
               
   
            { 
    }

For some reason in the second script hasCollided hasn’t been defined even though it should be via the first script. Any ideas?

Can you post the exact error message you’re seeing? If the line number in it is different than the above snippets, please renumber it to match where the line number in the above snippets.

i did end up fixing it by using the animation itself and enabling the collider under the properties bit of the animation

1 Like