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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Unity giving me an error when it shouldn't

Discussion in 'Scripting' started by HorizonHD, May 8, 2016.

  1. HorizonHD

    HorizonHD

    Joined:
    Jan 27, 2015
    Posts:
    11
    Ok so ive been following a tutorial on how to make inventorys by inScope studios, anyways I got to about an hour in the video, and I followed step by step, but unity gives me an error when I attempt to pick an item up. In his video he doesnt get the error and is able to pick the item up perfectly, So i spent 4+ hours going back and through my code and his code and I nailed down every line exactly but I still get the error. Heres the error:

    "NullReferenceException: Object reference not set to an instance of an object
    Slot.get_IsEmpty () (at Assets/Scripting/Slot.cs:18)
    Inventory.placeEmpty (.ItemScript item) (at Assets/Scripting/Inventory.cs:101)
    Inventory.AddItem (.ItemScript item) (at Assets/Scripting/Inventory.cs:87)
    KeyBinding.checkPickup () (at Assets/Scripting/KeyBinding.cs:75)
    KeyBinding.Update () (at Assets/Scripting/KeyBinding.cs:31)"

    And it targets this script specifically:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine.UI;
    5.  
    6. public class Slot : MonoBehaviour {
    7.  
    8.     private Stack<ItemScript> items;
    9.  
    10.     public Text stackTxt;
    11.  
    12.     public Sprite SlotEmpty;
    13.  
    14.     public Sprite SlotHighlight;
    15.  
    16.     public bool IsEmpty
    17.     {
    18.         get { return items.Count == 0; }      
    19.     }
    20.  
    21.     void start()
    22.     {
    23.        
    24.  
    25.         items = new Stack<ItemScript>();
    26.  
    27.         RectTransform slotRect = GetComponent<RectTransform>();
    28.         RectTransform txtRect = GetComponent<RectTransform>();
    29.  
    30.         int txtScaleFactor = (int)(slotRect.sizeDelta.x * 0.60); //calc scale factor
    31.         stackTxt.resizeTextMaxSize = txtScaleFactor; //set max scale factor
    32.         stackTxt.resizeTextMinSize = txtScaleFactor; //set min scale factor
    33.  
    34.  
    35.         txtRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, slotRect.sizeDelta.y);
    36.         txtRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, slotRect.sizeDelta.x);
    37.        
    38.  
    39.     }
    40.  
    41.     private void ChangeSprite(Sprite neutral, Sprite highlight)
    42.     {
    43.  
    44.  
    45.         GetComponent<Image>().sprite = neutral;
    46.  
    47.         SpriteState st = new SpriteState();
    48.         st.highlightedSprite = highlight;
    49.         st.pressedSprite = neutral;
    50.  
    51.         GetComponent<Button>().spriteState = st;
    52.  
    53.  
    54.  
    55.     }
    56.     void Update()
    57.     {
    58.  
    59.     }
    60.  
    61.     public void AddItem(ItemScript item)
    62.     {
    63.  
    64.  
    65.         items.Push(item);
    66.  
    67.         if (items.Count > 1) //Update stack number if > 1
    68.         {
    69.  
    70.             stackTxt.text = items.Count.ToString(); //writes the amount of items and converts to str
    71.  
    72.         }
    73.  
    74.         ChangeSprite(item.spriteNeutral, item.spriteHighlighted); //change sprite from slot to whatever we add
    75.  
    76.  
    77.     }
    78.  
    79. }
    80.  
    Note that
    public bool IsEmpty
    {
    get { return items.Count == 0; }
    }
    is where the problem is apparently coming from.
    I get the error whenever I collide with the object im trying to pick up, and i've even tried picking it up with a raycast, and I still receive the exact same error.


    P.S (Another problem im encountering is whenever I reboot this unity project my vertical axis on my FPS controller gets locked and I cant look up or down, my only work around was to delete the controller and put a new one in, even so its still incredibly annoying.)
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Start

    not

    start

    c# is case sensitive
     
    Kiwasi and HorizonHD like this.
  3. HorizonHD

    HorizonHD

    Joined:
    Jan 27, 2015
    Posts:
    11
    ARE YOU SERIOUS RIGHT NOW IM LITERALLY GOING TO DRINK BLEACH I SPENT 4 HOURS TRYING TO FIX THAT

    Ps thanks fam u just solved it
     
    BlazerLazer likes this.
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    fresh set of eyes :)
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Just a friendly piece of advice. This tactic is not known to improve programming ability.

    :p
     
    LeftyRighty likes this.