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

NullReferenceException issue

Discussion in 'Scripting' started by shin234, Apr 7, 2015.

  1. shin234

    shin234

    Joined:
    Jun 4, 2013
    Posts:
    7
    so ive spend the better part of the night digging through the forums looking for my answer but i cant seem to find it. so i come here. This is my very first project with maybe a week of practicing c# so i am assuming it is something simple i am overlooking but at this point i'm baffled.

    I seem to have everything right but am still getting this:

    NullReferenceException: Object reference not set to an instance of an object
    AUpgrade.Update () (at Assets/Scripts/AUpgrade.cs:19)

    Can anyone help? here's my code for that script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AUpgrade : MonoBehaviour {
    5.  
    6.     public AmmoClick click;
    7.     public UnityEngine.UI.Text iteminfo;
    8.     public float cost;
    9.     public float count;
    10.     public float power;
    11.     public string itemName;
    12.     private float baseCost;
    13.    
    14.     void Start(){
    15.         baseCost = cost;
    16.     }
    17.  
    18.     void Update(){
    19.         iteminfo.text = itemName + "\nCost: " + cost + "\nPower: X" + count;
    20.     }
    21.    
    22.     public void PurchasedUpgrade(){
    23.         if (click.cash >= cost) {
    24.             click.cash -= cost;
    25.             count += 1;
    26.             click.AmmoType += power;
    27.             cost = Mathf.Round (baseCost * Mathf.Pow (2.00f, count));
    28.         }
    29.     }
    30. }
    31.  
     
  2. Galf

    Galf

    Joined:
    Feb 24, 2013
    Posts:
    27
    It's complaining about line 19, where you set the text to iteminfo. Iteminfo is the thing that's null -- you need to drag a Text object to it in the editor
     
  3. shin234

    shin234

    Joined:
    Jun 4, 2013
    Posts:
    7
    it is here is a screenshot upload_2015-4-6_23-15-41.png
     
  4. shin234

    shin234

    Joined:
    Jun 4, 2013
    Posts:
    7
    and the script works 100% as intended it just fills the log with the error every time i start it
     
  5. Galf

    Galf

    Joined:
    Feb 24, 2013
    Posts:
    27
    Is it possible you have multiple of those scripts, and one doesn't have the text set? There's really nothing else in that expression that would cause an error, short of mayyyybe calling something count (it's possibly reserved). Try this: right before that statement, put this:
    Code (CSharp):
    1. Debug.Log("Item info is null? " + (iteminfo == null));
    If you are somehow using multiple scripts, that will alert you to that field beind null somewhere. If that only ever prints out false, I'm stumped :-/
     
  6. shin234

    shin234

    Joined:
    Jun 4, 2013
    Posts:
    7
    this is what i got . i got true but how do i trace it? i have looked through all my scripts and objects and noting is missing or repeated
     

    Attached Files:

  7. Galf

    Galf

    Joined:
    Feb 24, 2013
    Posts:
    27
    Great, so I'm not insane :). The script has to be attached to a GameObject in your scene in order to execute, so its just a matter of figuring out which one. Assuming you have unique names, your best bet is to replace our initial Debug statement with another one:
    Code (CSharp):
    1. Debug.Log(gameObject.name + " is null? " + (iteminfo == null));
     
  8. shin234

    shin234

    Joined:
    Jun 4, 2013
    Posts:
    7
    it says text and ammo is null but it it isint null in the script and every other script is defined properly as well as the objects. would this be a unity bug?

    Text is null? True
    UnityEngine.Debug:Log(Object)
    AUpgrade:Update() (at Assets/Scripts/AUpgrade.cs:19)

    Ammo is null? True
    UnityEngine.Debug:Log(Object)
    AUpgrade:Update() (at Assets/Scripts/AUpgrade.cs:19)
     
  9. shin234

    shin234

    Joined:
    Jun 4, 2013
    Posts:
    7
    FOUND IT! i had a stroke of minor genuis. i cut everything form the script then saved it and ran the game. it told me
    The referenced script on this Behaviour is missing! then i double clicked it and it took me to a text child object that apprently had the script attached to it for no reason. removed it and bingo all good sorry if i doubted you i swear i looked at that object a few times LOL
     
  10. shin234

    shin234

    Joined:
    Jun 4, 2013
    Posts:
    7
    I found another method to find an issue like this. Just in case someone comes across this thread looking for help. If you right click your script and click "find references in scene" it will only show the objects with that script attached in your hierarchy.
     
  11. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    You can also write t:Foo in the search bar over your hierarchy, and it will light up every GameObject with the script Foo on it. So in your case, it would be t:AUpgrade.

    The t: searches are a great help - you can search for any component - audio sources, lights, rigidbodies, custom scripts, you name it.