Search Unity

Feedback [HELP!!] NullReferenceException when trying to use a bool from another script in an if statment

Discussion in 'Scripting' started by arubin6, Oct 13, 2019.

  1. arubin6

    arubin6

    Joined:
    Sep 5, 2019
    Posts:
    3
    Error when I left click:
    "NullReferenceException: Object reference not set to an instance of an object
    SwordManager.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/Weapons/SwordManager.cs:26)"
    (In the posted code the error line is 27)


    In PlayerManager I set a bool, leftClick to true when I left click, then after 5 seconds I set it to false.
    In SwordManager, I am trying to make it so when the sword collides with the enemy (onTrigger) AND the player has left clicked (leftCick is true), then the enemy takes damage.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class SwordManager : MonoBehaviour
    7. {
    8.     private PlayerManager player;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.    
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.    
    20.     }
    21.     private void OnTriggerEnter(Collider other)
    22.     {
    23.         GameObject objectCollided = other.gameObject;
    24.         if (other.gameObject.tag == "enemy")
    25.         {
    26.             //Debug.Log("Collision with sword!");
    27.             if (player.leftClick == true)
    28.             {
    29.                 objectCollided.GetComponent<EnemyManager>().ChangeEnergy(-5);
    30.             }
    31.        
    32.         }
    33.     }
    34.  
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using UnityEngine.AI;
    7.  
    8. public class PlayerManager : MonoBehaviour
    9. {
    10.     public static PlayerManager instance; //singleton
    11.     public GameObject player;
    12.     public PlayerMovementManager movementManager;
    13.     private EnemyManager enemy;
    14.     private SwordManager sword;
    15.     public Animator anim;
    16.     public bool leftClick = false;
    17.  
    18.     //Health Variables
    19.     [SerializeField] private float currentEnergy;
    20.     [SerializeField] private float maxEnergy;
    21.     public Image EnergyBar;
    22.  
    23.     //Cheats
    24.     private bool invincible = false;
    25.  
    26.     private void Awake()
    27.     {
    28.         anim = GetComponent<Animator>();
    29.  
    30.         // Has the singleton not been created yet
    31.         if (instance == null)
    32.         {
    33.             instance = this;
    34.             DontDestroyOnLoad(gameObject);
    35.         }
    36.         else
    37.         {
    38.             Destroy(gameObject);
    39.         }
    40.     }
    41.     void Start()
    42.     {
    43.         movementManager = GetComponent<PlayerMovementManager>();
    44.         currentEnergy = 100f;
    45.         EnergyBar.fillAmount = 1;
    46.     }
    47.  
    48.     void Update()
    49.     {
    50.         CheckInvincibility();
    51.         PerformAttack();
    52.    
    53.     }
    54.  
    55.     void FixedUpdate()
    56.     {
    57.        //Debug.Log("Able to attack: " + leftClick);
    58.     }
    59.  
    60.     //Methods
    61.     void PerformAttack()
    62.     {
    63.         if (Input.GetMouseButton(0))
    64.         {
    65.             anim.SetInteger(HashIDs.slashCond_Int, 1);
    66.             leftClick = true;
    67.             Invoke("DisableAttack", 5f);
    68.         }
    69.         else
    70.         {
    71.             anim.SetInteger(HashIDs.slashCond_Int, 0);
    72.         }
    73.     }
    74.  
    75.     void DisableAttack()
    76.     {
    77.         leftClick = false;
    78.     }
    79. }
    80.  
     
  2. damonp

    damonp

    Joined:
    Dec 7, 2012
    Posts:
    8
    The way you have your code setup, in SwordManager, line 27, you'll want reference PlayerManager.instance
    Code (CSharp):
    1. if (PlayerManager.instance.leftClick == true)
     
    arubin6 likes this.