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

Can't access Boolean of another objects

Discussion in 'Scripting' started by Larpushka, Jul 31, 2020.

  1. Larpushka

    Larpushka

    Joined:
    Jan 6, 2015
    Posts:
    214
    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";

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CombatScript : MonoBehaviour
    6. {
    7.     public bool RightAttack;
    8.     bool Achiles;
    9.     Animator m_Animator;
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.                 m_Animator = gameObject.GetComponent<Animator>();
    15.  
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         if (Input.GetMouseButton(0))
    22.         {
    23.             RightAttack = true;
    24.              // Debug.Log("Pressed primary button.");
    25.         }
    26.         else
    27.         {
    28.             RightAttack = false;
    29.         }
    30.          if (Input.GetMouseButton(1))
    31.         {
    32.             Achiles = true;
    33.             //  Debug.Log("Pressed secondary button.");
    34.         }
    35.         else
    36.         {
    37.             Achiles = false;
    38.         }
    39.          
    40.           if (RightAttack == true)
    41.           {
    42.                           m_Animator.SetBool("RightAttack", true);
    43.  
    44.           }
    45.           else
    46.           {
    47.               m_Animator.SetBool("RightAttack", false);
    48.  
    49.           }
    50.          
    51.                 if (Achiles == true)
    52.           {
    53.                           m_Animator.SetBool("Achiles", true);
    54.  
    55.           }
    56.           else
    57.           {
    58.               m_Animator.SetBool("Achiles", false);
    59.  
    60.           }
    61.  
    62.          
    63.     }
    64. }
    65.  
    This is the script of the enemy where I'm trying to access it:


    Code (CSharp):
    1.  using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. public class EnemyScript : MonoBehaviour
    6. {
    7.   Animator m_Animator;
    8.    
    9.      public Transform Player;
    10.      int MoveSpeed = 1;
    11.      int MaxDist = 1;
    12.      bool Chase = false;
    13.      bool Stay = false;
    14.      bool Hurt;
    15.      bool IsAttacked;
    16.      public GameObject ThePlayer;
    17.    
    18. //    int MinDist = 5;
    19.      void Start()
    20.      {
    21.          Hurt = false;
    22.          Chase = false;
    23.    m_Animator = gameObject.GetComponent<Animator>();
    24.      }
    25.    
    26.       void OnTriggerEnter(Collider other)
    27.     {
    28.         if (other.tag == "Player")
    29.         {
    30.         Chase = true;
    31.  
    32.         }
    33.        
    34.                  if (other.tag == "Sword")
    35.                  {
    36.                      IsAttacked = ThePlayer.GetComponent("Combat Script").RightAttack;
    37.                      Hurt = true;  
    38. Debug.Log("OUCH!.");                    
    39.                  }
    40.                
    41.            
    42.            
    43.              if (Hurt == true)
    44.              {
    45.                 m_Animator.SetBool("Hurt", true);
    46.              }
    47.     }
    48.    
    49.     void Update()
    50.     {
    51.         //Debug.Log(Chase);
    52.         if (Chase==true)
    53.         {
    54.               transform.LookAt(Player);
    55.             m_Animator.SetBool("IsWalking", true);
    56.                 transform.position += transform.forward * MoveSpeed * Time.deltaTime;
    57.         }
    58.        
    59.                       if (Vector3.Distance(transform.position, Player.position) < MaxDist)
    60.                       {
    61.                           Chase = false;
    62.                                       m_Animator.SetBool("IsWalking", false);
    63.  
    64.                            m_Animator.SetTrigger("EnemyAttack");
    65.                       }
    66.                      
    67.                       if (Vector3.Distance(transform.position, Player.position) > MaxDist && Stay == true)
    68.                       {
    69.                           Chase = true;
    70.                       }      
    71.        
    72.     }
    73.        
    74.                  
    75. //Here Call any function U want Like Shoot at here or something
    76.            
    77.            
    78.            
    79.                 //
    80.            
    81.  
    82.             void OnTriggerExit(Collider other)
    83.         {
    84.          if (other.tag == "Player")
    85.          {
    86.              m_Animator.SetBool("IsWalking", false);
    87.              Chase = false;
    88.             Stay = false;
    89.  
    90.          }
    91.          if (other.tag == "Sword")
    92.          {
    93.          m_Animator.SetBool("Hurt", false);
    94.             }
    95.    
    96.         }
    97.              void OnTriggerStay(Collider other)
    98.         {
    99.          if (other.tag == "Player")
    100.          {
    101.            
    102.              Stay = true;
    103.  
    104.  
    105.          }
    106.         }
    107.    
    108. }
    109.          
    110.            
    111.            
    112.        
    113.  
     
  2. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    175
    Code (CSharp):
    1.  
    2.  IsAttacked = ThePlayer.GetComponent("Combat Script").RightAttack;
    To
    Code (CSharp):
    1.  
    2. IsAttacked = ThePlayer.GetComponent<CombatScript>().RightAttack;
     
    Larpushka likes this.
  3. Larpushka

    Larpushka

    Joined:
    Jan 6, 2015
    Posts:
    214
    YES! I've been wracking my head for a long time! Thank you :)