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

Bug i have no clue why this is getting a NullReferenceExeption

Discussion in 'Scripting' started by Boombop112, May 22, 2023.

  1. Boombop112

    Boombop112

    Joined:
    Jan 25, 2023
    Posts:
    2
    im trying to disable a game object that is public and it wont stop giving me a NullReferenceExeption error
    here is the error script :
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;


    public class hpmanager : MonoBehaviour
    {
    public GameObject Green1, Green2, Green3, Green4, Green5, Green6, Green7, Green8, Green9, Green10;

    public void enemy1()
    {
    if(Green1.activeInHierarchy)
    {
    Green1.SetActive(false);
    }
    if(Green2.activeInHierarchy)
    {
    Green2.SetActive(false);
    }
    if(Green3.activeInHierarchy)
    {
    Green3.SetActive(false);
    }
    if(Green4.activeInHierarchy)
    {
    Green4.SetActive(false);
    }
    if(Green5.activeInHierarchy)
    {
    Green5.SetActive(false);
    }
    if(Green6.activeInHierarchy)
    {
    Green6.SetActive(false);
    }
    if(Green7.activeInHierarchy)
    {
    Green7.SetActive(false);
    }
    if(Green8.activeInHierarchy)
    {
    Green8.SetActive(false);
    }
    if(Green9.activeInHierarchy)
    {
    Green9.SetActive(false);
    }
    if(Green10.activeInHierarchy)
    {
    Green10.SetActive(false);
    }

    }
    public void Heal()
    {


    }
    }
    Heal is empty because i am waiting to fix my current issue HELP.png AHHHH.png
    hpp.png
    (this uses multiple green images / gameobjects to create said hp bar)

    all i know is that i SHOULD not be getting a nullReferenceError

    and this is what it should look like once disabled
    what it should look like.png
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,560
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,494
    This is most likely again one of the classical mistakes of calling a method on a prefab instead of the actual object in the scene. Prefab assets are also objects but they don't live in the scene. However you can still call methods on them.

    Try adding a Debug.Log at the very beginning of your "enemy1" method and add a "context" object as second argument to Debug.Log. Now when you click on the log message in the console, the Unity editor will highlight / "ping" that context object in the hierarchy or project view.

    Code (CSharp):
    1. Debug.Log("enemy1()", gameObject);
    So click on the message, see on which object you're calling this method on and check if that object has those references set. I guess not. So the issue is most likely at the place where you actually call this method.
     
  4. Boombop112

    Boombop112

    Joined:
    Jan 25, 2023
    Posts:
    2
    i have found on what is null its my game object when the script starts it is not but when it goes through enemy1 it becomes null and i dont know why i tried looking it up and found some information but none of it fixes my issue. also i fixed up my script so that its more easy on the eyes and changed the game objects to one image (im going to try and use fill amount) this is what it looks like now:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class hpmanager : MonoBehaviour
    7. {
    8.     public Image Green;
    9.  
    10.     public void Start()
    11.     {
    12.        if(Green == null)
    13.         {
    14.             Debug.Log("start null");
    15.         }
    16.     }
    17.     public void enemy1()
    18.     {
    19.         if (Green == null)
    20.         {
    21.             Debug.Log("null");
    22.         }
    23.     }
    24.     public void Heal()
    25.     {
    26.    
    27.    
    28.     }
    29. }

    incase if i need it here i will also post my enemy script that is referencing this in case the error is in that script despite me not thinking it is :
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Net.Security;
    4. using UnityEngine;
    5. using UnityEngine.AI;
    6. using UnityEngine.UIElements;
    7.  
    8. public class enemy : MonoBehaviour
    9. {
    10.     public Transform player, me;
    11.     public LayerMask whatPlayer, allother;
    12.     //attac
    13.     public float timeBetweenAttacks;
    14.     //State
    15.     public float attackRange;
    16.     public bool playerInAttackRange, alreadyAttacked, canattak;
    17.     private void Update()
    18.     {
    19.         playerInAttackRange = Physics.CheckSphere(me.position, attackRange, whatPlayer);
    20.         if (playerInAttackRange == true)
    21.         {
    22.             AttackPlayer();
    23.         }
    24.     }
    25.  
    26.     private void AttackPlayer()
    27.     {
    28.         me.LookAt(player);
    29.         if(Physics.Linecast(me.position, player.position,allother))
    30.         {
    31.             canattak= false;
    32.         }
    33.         else
    34.         {
    35.             canattak= true;
    36.         }
    37.    
    38.          if(canattak==true)
    39.         {
    40.             if (Physics.Linecast(me.position, player.position) && !alreadyAttacked)
    41.             {
    42.                 Debug.DrawLine(me.position, player.position, Color.red);
    43.            
    44.                 hpmanager attack = new hpmanager();
    45.            
    46.                 ///attack
    47.                 attack.enemy1();
    48.                 ///
    49.                 alreadyAttacked = true;
    50.                 Invoke(nameof(ResetAttack), timeBetweenAttacks);
    51.             }
    52.         }
    53.    
    54.     }
    55.     private void ResetAttack()
    56.     {
    57.         alreadyAttacked= false;
    58.     }
    59.     private void OnDrawGizmosSelected()
    60.     {
    61.         Gizmos.color = Color.red;
    62.         Gizmos.DrawWireSphere(me.position, attackRange);
    63.     }
    64.  
    65. }
    TWEATWEA.png
     
    Last edited: May 23, 2023
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,494
    What do you think this line does? This is just plain wrong and you even get a warning in your console about that. In that line you create a new hpmanager with the "new" keyword. MonoBehaviours / Components can not be created with new. Even if you could that new instance would have nothing to do with your other instance where you set all those variables. So it seems you have a fundamental misunderstanding how components work. Components have to be attached to gameobjects in the scene.

    You also have 3 audio listener and 2 event systems in your scene. You really need to clean up your scene.

    We don't know where your "hpmanager" script is attached to since you only showed a screenshot of that script. So when your enemy wants to call a method on that script, your enemy needs a reference to that particular instance. This could be done through a public variable that is set to that hpmanager that is in the scene, or you might want to turn that hpmanager class into a singleton. We simply don't have enough information about your project setup to make clear suggestions. All we can say is that what you're currently doing can't work.
     
    Kurt-Dekker likes this.