Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug NullReferenceException: Object reference not set to an instance of an object

Discussion in 'Editor & General Support' started by PanSageYT, Jun 11, 2021.

  1. PanSageYT

    PanSageYT

    Joined:
    Mar 16, 2021
    Posts:
    11
    I am making Tower Defense based on Brackeys tutorial.
    I didn't do nodes, so I needed to make my own scripts

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5. using UnityEngine.UI;
    6. using CodeMonkey;
    7. using CodeMonkey.Utils;
    8.  
    9. public class Tower : MonoBehaviour
    10. {
    11.     [Header("Prefabs")]
    12.     public GameObject ShooterBeanPF;
    13.     public GameObject GatlingBeanPF;
    14.     public GameObject BonkerBeanPF;
    15.     public GameObject MMBeanPF;
    16.     public GameObject SniperBeanPF;
    17.     public GameObject NinjaBeanPF;
    18.  
    19.     private GameObject tempgo;
    20.  
    21.     public Animator camAnim;
    22.  
    23.     private Vector3 placeTowerHere;
    24.  
    25.     [Header("Panel")]
    26.     public GameObject upgradePanel;
    27.    
    28.     [Header("Upgrade Panel Text")]
    29.     public GameObject ur;
    30.     public GameObject ufs;
    31.     public GameObject sell;
    32.  
    33.     [Header("Leave me alone ok?")]
    34.     Vector3 clickPosBack;
    35.  
    36.     public Camera cam;
    37.  
    38.     public int canskip;
    39.     public bool isGrounded;
    40.     public bool isGrounded2;
    41.  
    42.     bool click = true;
    43.     bool upgradePanelOn = false;
    44.  
    45.     Vector3 beantemp;
    46.     // Update is called once per frame
    47.  
    48.     void Start()
    49.     {
    50.         upgradePanel.SetActive(false);
    51.         upgradePanelOn = false;
    52.     }
    53.  
    54.     void Update()
    55.     {
    56.         if (upgradePanelOn == true && click == false)
    57.         {
    58.             upgradePanelOn = false;
    59.             upgradePanel.SetActive(false);
    60.         }
    61.     }
    62.  
    63.     public void CamShake()
    64.     {
    65.         camAnim.SetTrigger("shake");
    66.         canskip = 0;
    67.     }
    68.  
    69.     void OnGUI()
    70.     {
    71.         if (Shop.selected == 0) { return; }
    72.         // GETTING POSITION
    73.  
    74.         if (Input.GetMouseButtonDown(0) && click == true)
    75.         {
    76.             click = false;
    77.             Vector3 clickPos = -Vector3.one;
    78.  
    79.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    80.             RaycastHit hit;
    81.  
    82.             if (Physics.Raycast(ray, out hit))
    83.             {
    84.                 clickPos = hit.point;
    85.                 clickPosBack = clickPos;
    86.             }
    87.  
    88.             // CHECKING IF U CAN
    89.  
    90.  
    91.             Collider[] colliders = Physics.OverlapSphere(clickPos, 3f);
    92.  
    93.             foreach (Collider isthere in colliders)
    94.             {
    95.                 if (isthere.gameObject.name == "Podloze" || isthere.gameObject.tag == "Enemy") { canskip += 1; }
    96.                 if (isthere.gameObject.tag == "Bean") {
    97.                     upgradePanel.SetActive(true);
    98.                     upgradePanelOn = true;
    99.  
    100.                     beantemp = isthere.gameObject.transform.position;
    101.                     upgradePanel.transform.position = beantemp;
    102.  
    103.                     tempgo = isthere.gameObject;
    104.  
    105.                     ur.GetComponent<Text>().text = "Upgrade Fire Range " + tempgo.GetComponent<IDselect>().mutl * 100 + " (" + tempgo.GetComponent<IDselect>().nr + ")";
    106.                     ufs.GetComponent<Text>().text = "Upgrade Fire Speed " + tempgo.GetComponent<IDselect>().mutl * 100 + " (" + tempgo.GetComponent<IDselect>().nfs + ")";
    107.                     sell.GetComponent<Text>().text = "Sell " + tempgo.GetComponent<IDselect>().mutl * 90 + " (Cannot undo)";
    108.                     return;
    109.                 }
    110.             }
    111.  
    112.             // Right here \/ if I place one of them, I get error NullReferenceException: Object reference not set to an instance of an object
    113.  
    114.             if (colliders.Length - canskip == 0 && Shop.selected == 1 && GameHandler.Money >= 200) { tempgo = Instantiate(ShooterBeanPF, new Vector3(clickPos.x, clickPos.y + 1, clickPos.z), Quaternion.identity); GameHandler.Money -= 200; tempgo.GetComponent<IDselect>().mutl = 2; tempgo.GetComponent<IDselect>().idsel = 1; }
    115.             else if (colliders.Length - canskip == 0 && Shop.selected == 2 && GameHandler.Money >= 500) { tempgo = Instantiate(GatlingBeanPF, new Vector3(clickPos.x, clickPos.y + 1, clickPos.z), Quaternion.identity); GameHandler.Money -= 500; tempgo.GetComponent<IDselect>().mutl = 5; tempgo.GetComponent<IDselect>().idsel = 2; }
    116.             else if (colliders.Length - canskip == 0 && Shop.selected == 3 && GameHandler.Money >= 400) { tempgo = Instantiate(BonkerBeanPF, new Vector3(clickPos.x, clickPos.y + 1, clickPos.z), Quaternion.identity); GameHandler.Money -= 400; tempgo.GetComponent<IDselect>().mutl = 4; tempgo.GetComponent<IDselect>().idsel = 3; }
    117.             else if (colliders.Length - canskip == 0 && Shop.selected == 4 && GameHandler.Money >= 1000) { tempgo = Instantiate(MMBeanPF, new Vector3(clickPos.x, clickPos.y + 1, clickPos.z), Quaternion.identity); GameHandler.Money -= 1000; tempgo.GetComponent<IDselect>().mutl = 10; tempgo.GetComponent<IDselect>().idsel = 4; }
    118.             else if (colliders.Length - canskip == 0 && Shop.selected == 5 && GameHandler.Money >= 300) { tempgo = Instantiate(SniperBeanPF, new Vector3(clickPos.x, clickPos.y + 1, clickPos.z), Quaternion.identity); GameHandler.Money -= 300; tempgo.GetComponent<IDselect>().mutl = 3; tempgo.GetComponent<IDselect>().idsel = 5; }
    119.             else if (colliders.Length - canskip == 0 && Shop.selected == 6 && GameHandler.Money >= 500) { tempgo = Instantiate(NinjaBeanPF, new Vector3(clickPos.x, clickPos.y + 1, clickPos.z), Quaternion.identity); GameHandler.Money -= 500; tempgo.GetComponent<IDselect>().mutl = 5; tempgo.GetComponent<IDselect>().idsel = 6; }
    120.             else if (colliders.Length - canskip != 0 && Shop.selected != 0 && GameHandler.Money <= 200) { CamShake(); return; }
    121.             else if (Shop.selected == 0) { return; }
    122.             else { CamShake(); return; }
    123.         }
    124.  
    125.         if (Input.GetMouseButtonUp(0))
    126.         {
    127.             click = true;
    128.         }
    129.     }
    130.  
    131.     public void SellBtn()
    132.     {
    133.         Destroy(tempgo);
    134.         GameHandler.Money += tempgo.GetComponent<IDselect>().mutl * 90;
    135.     }
    136.  
    137.     public void UpgradeFrBtn()
    138.     {
    139.         if (GameHandler.Money >= tempgo.GetComponent<IDselect>().mutl * 100) { CamShake(); return; }
    140.         tempgo.GetComponent<IDselect>().nr += 1;
    141.         GameHandler.Money -= tempgo.GetComponent<IDselect>().mutl * 100;
    142.     }
    143.  
    144.     public void UpgradeFsBtn()
    145.     {
    146.         if (GameHandler.Money >= tempgo.GetComponent<IDselect>().mutl * 100) { CamShake(); return; }
    147.         tempgo.GetComponent<IDselect>().nfs += 1;
    148.         GameHandler.Money -= tempgo.GetComponent<IDselect>().mutl * 100;
    149.     }
    150. }
    151.  
    Please help
     
    Last edited: Jun 11, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    The answer is always the same... ALWAYS. It is the single most common error ever.

    Don't waste your life spinning around and round on this error. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception
    - also known as: Object reference not set to an instance of an object

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.

    Here is a clean analogy of the actual underlying problem of a null reference exception:

    https://forum.unity.com/threads/nul...n-instance-of-an-object.1108865/#post-7137032
     
  3. PanSageYT

    PanSageYT

    Joined:
    Mar 16, 2021
    Posts:
    11
    my bad, only some towers had IDselect script