Can't access Boolean of another objects

Got 1 player, 1 enemy.
Trying to access the bool “RightAttack” (it’s for a sword fighting system, I want the enemy to know when he’s being swung at…)

On the enemy’s script I’m getting the error:
Assets\EnemyScript.cs(39,60): error CS1061: ‘Component’ does not contain a definition for ‘RightAttack’ and no accessible extension method ‘RightAttack’ accepting a first argument of type ‘Component’ could be found (are you missing a using directive or an assembly reference?)

This is the script of the player where I declare “RightAttack”;

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

public class CombatScript : MonoBehaviour
{
    public bool RightAttack;
    bool Achiles;
    Animator m_Animator;

    // Start is called before the first frame update
    void Start()
    {
                m_Animator = gameObject.GetComponent<Animator>();

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            RightAttack = true;
             // Debug.Log("Pressed primary button.");
        }
        else
        {
            RightAttack = false;
        }
         if (Input.GetMouseButton(1))
        {
            Achiles = true;
            //  Debug.Log("Pressed secondary button.");
        }
        else
        {
            Achiles = false;
        }
         
          if (RightAttack == true)
          {
                          m_Animator.SetBool("RightAttack", true);

          }
          else
          {
              m_Animator.SetBool("RightAttack", false);

          }
         
                if (Achiles == true)
          {
                          m_Animator.SetBool("Achiles", true);

          }
          else
          {
              m_Animator.SetBool("Achiles", false);

          }

         
    }
}

This is the script of the enemy where I’m trying to access it:

 using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class EnemyScript : MonoBehaviour
{
  Animator m_Animator;
   
     public Transform Player;
     int MoveSpeed = 1;
     int MaxDist = 1;
     bool Chase = false;
     bool Stay = false;
     bool Hurt;
     bool IsAttacked;
     public GameObject ThePlayer;
    
//    int MinDist = 5;
     void Start()
     {
         Hurt = false;
         Chase = false;
   m_Animator = gameObject.GetComponent<Animator>();
     }
    
      void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Player")
        {
        Chase = true;

        }
       
                 if (other.tag == "Sword")
                 {
                     IsAttacked = ThePlayer.GetComponent("Combat Script").RightAttack;
                     Hurt = true;   
Debug.Log("OUCH!.");                    
                 }
                
            
            
             if (Hurt == true)
             {
                m_Animator.SetBool("Hurt", true);
             }
    }
    
    void Update()
    {
        //Debug.Log(Chase);
        if (Chase==true)
        {
              transform.LookAt(Player);
            m_Animator.SetBool("IsWalking", true);
                transform.position += transform.forward * MoveSpeed * Time.deltaTime;
        }
       
                      if (Vector3.Distance(transform.position, Player.position) < MaxDist)
                      {
                          Chase = false;
                                      m_Animator.SetBool("IsWalking", false);

                           m_Animator.SetTrigger("EnemyAttack");
                      }
                     
                      if (Vector3.Distance(transform.position, Player.position) > MaxDist && Stay == true)
                      {
                          Chase = true;
                      }       
       
    }
       
                  
//Here Call any function U want Like Shoot at here or something
            
           
            
                //
           

            void OnTriggerExit(Collider other)
        {
         if (other.tag == "Player")
         {
             m_Animator.SetBool("IsWalking", false);
             Chase = false;
            Stay = false;

         }
         if (other.tag == "Sword")
         {
         m_Animator.SetBool("Hurt", false);
            }
    
        }
             void OnTriggerStay(Collider other)
        {
         if (other.tag == "Player")
         {
           
             Stay = true;


         }
        }
   
}
 IsAttacked = ThePlayer.GetComponent("Combat Script").RightAttack;

To

IsAttacked = ThePlayer.GetComponent<CombatScript>().RightAttack;
1 Like

YES! I’ve been wracking my head for a long time! Thank you :slight_smile: