Search Unity

(Solved)NullReferenceException

Discussion in 'Scripting' started by Lutherion, May 28, 2017.

  1. Lutherion

    Lutherion

    Joined:
    May 28, 2017
    Posts:
    25
    Hello and im making a Tower defense game, but when you click on one of the Places you can build without a turret selected i get this error

    NullReferenceException
    UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineObjectBindings.gen.cs:53)
    UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:156)
    UnityEngine.Object.Instantiate[GameObject] (UnityEngine.GameObject original, Vector3 position, Quaternion rotation) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:206)
    BuildManager.BuildTurretOn (.Node node) (at Assets/Scripts/BuildManager.cs:33)
    Node.OnMouseDown () (at Assets/Scripts/Node.cs:45)
    UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Maybe something you're instantiating is null or some other value in that area of code. If you check that section, you may find your answer. If you're still lost, you could post your code and someone might notice what's potentially wrong.
     
  3. Lutherion

    Lutherion

    Joined:
    May 28, 2017
    Posts:
    25
    This is the 3 scripts i think is the problem

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class BuildManager : MonoBehaviour {
    public static BuildManager instance;
    public GameObject LaserRiflePrefab;
    public GameObject MissileLauncherPrefab;
    public GameObject LaserBeamPrefab;
    public TurretBlueprint TurretToBuild;
    public bool CanBuild{ get { return TurretToBuild != null; } }
    void Awake()
    {
    if(instance != null)
    {
    Debug.Log("More than 1 Buildmanager");
    return;
    }
    instance = this;
    }
    public void BuildTurretOn(Node node)
    {
    if (PlayerStats.Money < TurretToBuild.Cost)
    {
    Debug.Log("Not enough Money");
    return;
    }
    PlayerStats.Money -= TurretToBuild.Cost;
    GameObject Turret = (GameObject)Instantiate(TurretToBuild.Prefab, node.GetBuildPosition(), Quaternion.identity);
    node.CurrentTurret = Turret;
    }
    public void SelectTurretToBuild(TurretBlueprint turret)
    {
    TurretToBuild = turret;
    }
    }




    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.EventSystems;
    public class Node : MonoBehaviour {
    public Color HoverColor;
    public Vector3 PositionOffset;
    [Header("Optional")]
    public GameObject CurrentTurret;
    private Renderer Rend;
    private Color StartColor;
    BuildManager buildManager;

    void Start()
    {
    Rend = GetComponent<Renderer>();
    StartColor = Rend.material.color;
    buildManager = BuildManager.instance;
    }
    public Vector3 GetBuildPosition()
    {
    return transform.position + PositionOffset;
    }
    void OnMouseDown()
    {
    if (EventSystem.current.IsPointerOverGameObject())
    return;
    if (CurrentTurret != null)
    {
    return;
    }
    if (!buildManager.CanBuild)
    {
    return;
    }
    buildManager.BuildTurretOn(this);
    }
    void OnMouseEnter()
    {
    if (EventSystem.current.IsPointerOverGameObject())
    return;

    if (!buildManager.CanBuild)
    return;

    Rend.material.color = HoverColor;
    }
    void OnMouseExit()
    {
    Rend.material.color = StartColor;
    }
    }





    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class Shop : MonoBehaviour {
    public TurretBlueprint LaserRifle;
    public TurretBlueprint MissileLauncher;
    public TurretBlueprint LaserBeam;
    BuildManager buildManager;
    void Start()
    {
    buildManager = BuildManager.instance;
    }
    void Update()
    {
    if (Input.GetKeyDown("1"))
    {
    SelectLaserRifle();
    }
    if (Input.GetKeyDown("2"))
    {
    SelectMissileLauncher();
    }
    if (Input.GetKeyDown("3"))
    {
    SelectLaserBeam();
    }
    }
    public void SelectLaserRifle()
    {
    Debug.Log("Selected LaserRifle");
    buildManager.SelectTurretToBuild(LaserRifle);
    }
    public void SelectMissileLauncher()
    {
    Debug.Log("Selected MissileLauncher");
    buildManager.SelectTurretToBuild(MissileLauncher);
    }
    public void SelectLaserBeam()
    {
    Debug.Log("Selected LaserBeam");
    buildManager.SelectTurretToBuild(LaserBeam);
    }
    }
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    https://forum.unity3d.com/threads/using-code-tags-properly.143875/

    Use code tags.

    And since you are getting an error, double click it and it should open the script and take you to the proper line where the null is occurring. In this case, looks like buildManager line 33. And since it does mention instantiate, it's probably your object that you are trying to instantiate. But again, you should easily be able to solve this by looking at that line when you double click the error and seeing what parts might be null.
     
  5. Lutherion

    Lutherion

    Joined:
    May 28, 2017
    Posts:
    25
    it's null because no turret is selected and that is the way it should be, and it still highlights the Square you can build on without a turret selected, but it should'n highlight it, with that i mean when you hold over a place you can build it gets Black and White Again when your not over it
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Please repost the relevant code parts using code tags (see @Brathnann 's response for the link) and indicate which line has the error , to refresh anyone's memory who is reading this :)
     
  7. Lutherion

    Lutherion

    Joined:
    May 28, 2017
    Posts:
    25
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BuildManager : MonoBehaviour {
    6.     public static BuildManager instance;
    7.     public GameObject LaserRiflePrefab;
    8.     public GameObject MissileLauncherPrefab;
    9.     public GameObject LaserBeamPrefab;
    10.  
    11.     public TurretBlueprint TurretToBuild;
    12.  
    13.     public bool CanBuild{ get { return TurretToBuild != null; } }
    14.  
    15.     void Awake()
    16.     {
    17.         if(instance != null)
    18.         {
    19.             Debug.Log("More than 1 Buildmanager");
    20.             return;
    21.         }
    22.         instance = this;
    23.     }
    24.  
    25.     public void BuildTurretOn(Node node)
    26.     {
    27.         if (PlayerStats.Money < TurretToBuild.Cost)
    28.         {
    29.             Debug.Log("Not enough Money");
    30.             return;
    31.         }
    32.         PlayerStats.Money -= TurretToBuild.Cost;
    33.        <This Line Is Broke>: GameObject Turret = (GameObject)Instantiate(TurretToBuild.Prefab, node.GetBuildPosition(), Quaternion.identity);
    34.         node.CurrentTurret = Turret;
    35.     }
    36.  
    37.     public void SelectTurretToBuild(TurretBlueprint turret)
    38.     {
    39.         TurretToBuild = turret;
    40.     }
    41. }
    42.  
     
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    I don't understand your game enough to really comment on if what you are doing is the right approach. But if an object must be null but you need to use it, you are going to get an error. So you need to check if the value is null before you try to use it. If you can't do this, then your code needs some work because you can't just have a null value that you try to use (example you can't instantiate a null gameobject).

    The only solution I can think of is you have a default gameobject that is empty and use that as a placeholder when the object needs to be null, but otherwise, checking for null should be enough.
     
  9. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    Since you've indicated that the following line is the problem:

    Code (csharp):
    1. GameObject Turret = (GameObject)Instantiate(TurretToBuild.Prefab, node.GetBuildPosition(), Quaternion.identity);  
    Because you've made it past TurretToBuild.Cost, I'm going to assume it's node that's NULL.

    So as @Brathnann suggested, it may be enough to just throw an error if node is null because you shouldn't allow building on a null node. Or investigate why it's null to begin with if "that shouldn't happen".
     
  10. Lutherion

    Lutherion

    Joined:
    May 28, 2017
    Posts:
    25
    found the problem, the problem was that i made a public variable instead of a private variable...
     
  11. patricio2142

    patricio2142

    Joined:
    Mar 7, 2020
    Posts:
    4
    I'm having the same problem, can you explain which variable have you changed to private??
     
    talhaacikel3 and URPian like this.