Search Unity

NullreferenceException: Object reference not set

Discussion in 'Scripting' started by ladman0311, Aug 27, 2012.

  1. ladman0311

    ladman0311

    Joined:
    Aug 16, 2012
    Posts:
    46
    Ok so i have gone up and down this script trying to find out what is causing this error. i cant find the cause anywhere. the error is being caused on line 114 and 112 in my code. but it does not specipy what object reference is not being instantiated.









     
  2. Democre

    Democre

    Joined:
    Mar 31, 2010
    Posts:
    345
    Your "hit" RaycastHit object is never being assigned. Try using Physics.Raycast(ray, out hit, 10) instead.
     
  3. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    lol@posting 4 pages of screenshots...
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
  5. ladman0311

    ladman0311

    Joined:
    Aug 16, 2012
    Posts:
    46
  6. ladman0311

    ladman0311

    Joined:
    Aug 16, 2012
    Posts:
    46
    i did that and it still did not work
     
  7. parandham03

    parandham03

    Joined:
    May 2, 2012
    Posts:
    174
  8. rob4097

    rob4097

    Joined:
    May 15, 2012
    Posts:
    50
    I believe you need to access hit.collider.gameObject.tag at line 112 - you've skipped the "collider" part. :)

    If that doesn't work, your issue may be more related to the ray not hitting anything - are you sure it's casting in the proper direction, and of adequate length? It seems like your issue is that whatever object you're trying to check the tag of doesn't exist, which is indicative of either code trying to look in the wrong place (skipping the collider part) or else code looking in the right place for an object that isn't there (hit not being assigned).
     
    Last edited: Aug 27, 2012
  9. ladman0311

    ladman0311

    Joined:
    Aug 16, 2012
    Posts:
    46
    changed it to if(hit.collider.gameObject.tag == "Ablock") that also did not work. Now i'm getting the error:


    NullReferenceException: Object reference not set to an instance of an object
    RandomWordGeneratro.Update () (at Assets/MyScripts/RandomWordGeneratro.cs:112)
     
  10. ladman0311

    ladman0311

    Joined:
    Aug 16, 2012
    Posts:
    46
    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class RandomWordGeneratro : MonoBehaviour
    7. {
    8.    
    9. //======================================================================
    10.    
    11.     #region Declariations
    12.    
    13.     bool isAppleWordComplete    = false;
    14.     bool isOrangeWordComplete   = false;
    15.     bool isPearWordComplete     = false;
    16.     bool isDestroyed            = false;
    17.    
    18.     string appleString          = "Apple";
    19.    
    20.     enum GameState { spellApple, spellOrange, spellPear }
    21.    
    22.     GameState currentState;
    23.    
    24.     public Texture2D[] lettersArray;
    25.    
    26.     public Texture2D[] wordsArray;
    27.  
    28.     #endregion
    29.    
    30. //======================================================================
    31.    
    32.     #region OnGUI
    33.  
    34.     void OnGUI()
    35.     {
    36.         if( currentState == GameState.spellApple)
    37.         {
    38.             //Rect (left, top, width, height)
    39.            
    40.             GUI.DrawTexture(new Rect(10, 10,  200, 100), wordsArray[0]);
    41.         }
    42.         else if( currentState == GameState.spellOrange)
    43.         {
    44.             //Rect (left, top, width, height)
    45.                
    46.             GUI.DrawTexture(new Rect(10, 10,  200, 100), wordsArray[1]);
    47.         }
    48.         else
    49.         {
    50.             //Rect (left, top, width, height)
    51.                
    52.             GUI.DrawTexture(new Rect(10, 10,  200, 100), wordsArray[2]);
    53.         }
    54.        
    55.     }
    56.    
    57.     #endregion
    58.    
    59. //======================================================================
    60.    
    61.     #region Initilize (AKA Start)
    62.    
    63.     // Use this for initialization
    64.     void Start ()
    65.     {
    66.         int rnd = Random.Range(0, 3);   // Generate a random number to choose what state to be in
    67.        
    68.         if(rnd == 0)
    69.         {
    70.             currentState = GameState.spellApple;
    71.             Debug.Log (" In spellApple State ");
    72.         }
    73.         else if(rnd == 1)
    74.         {
    75.             currentState = GameState.spellOrange;
    76.             Debug.Log (" In spellOrange State ");
    77.         }
    78.         else if(rnd == 2)
    79.         {
    80.             currentState = GameState.spellPear;
    81.             Debug.Log (" In spellPear State ");
    82.         }
    83.  
    84.     }
    85.    
    86.     #endregion
    87.    
    88. //======================================================================
    89.    
    90.     #region Update
    91.    
    92.     // Update is called once per frame
    93.     void Update ()
    94.     {
    95.        
    96. //======================================================================
    97.            
    98.         #region GameStateApple
    99.        
    100.         switch (currentState)
    101.         {
    102.            
    103.             case GameState.spellApple:
    104.  
    105.                 if(Input.GetMouseButton(0))
    106.                 {
    107.                     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    108.                     RaycastHit hit;
    109.        
    110.                     if(Physics.Raycast(ray, 10))
    111.                     {
    112.                         // Conditionals for Hitting the proper blocks
    113.                
    114.                         if(hit.collider.gameObject.tag == "Ablock")
    115.                         {
    116.                             if( appleString.Contains("A")  gameObject.tag == "Ablock" )
    117.                             {
    118.                                 Destroy(hit.transform.gameObject);
    119.                        
    120.                                 GUI.DrawTexture(new Rect(10, 50,  200, 100), lettersArray[0]);
    121.                        
    122.                                 isDestroyed = true;
    123.                        
    124.                                 appleString = appleString.Remove (0,1);
    125.                        
    126.                                 Debug.Log(appleString);
    127.                        
    128.                                     if(isDestroyed == true)
    129.                                     {
    130.                                         isAppleWordComplete = true;
    131.                                     }
    132.                             }
    133.                
    134.                         }
    135.                     }
    136.                 }
    137.            
    138.                 // when all the blocks are destroyed complete the word and change the state
    139.            
    140.                 if(isAppleWordComplete == true)
    141.                 {
    142.                
    143.                         currentState = GameState.spellOrange;
    144.                 }
    145.        
    146.         break;
    147.            
    148.     #endregion
    149.            
    150. //======================================================================
    151.            
    152.         #region GameStateOrange
    153.            
    154.         case GameState.spellOrange:
    155.  
    156.             if(isOrangeWordComplete == true)
    157.             {
    158.                 currentState = GameState.spellPear;
    159.             }
    160.  
    161.             break;
    162.            
    163.     #endregion
    164.            
    165. //======================================================================
    166.            
    167.         #region GameStatePear
    168.            
    169.         case GameState.spellPear:
    170.            
    171.             if(isPearWordComplete == true)
    172.             {
    173.                 currentState = GameState.spellApple;
    174.             }
    175.  
    176.             break;
    177.            
    178.         }
    179.     }
    180.    
    181.     #endregion
    182.    
    183. //======================================================================
    184.    
    185.     #endregion
    186.    
    187. //======================================================================
    188.  
    189.     #region EndBracde
    190.    
    191. }
    192.  
    193.     #endregion
    194.  
    195. //======================================================================
    196.  
    197.  
     
  11. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    I would suggest waiting until you're better at scripting yourself before 'helping'.

    1. Original code using transform.gameObject.tag was correct.

    2. The line of code in question wont be hit unless the raycast hits something


    With assigning hit, I cant see anything wrong with the code. Perhaps you could post an updated version of your code correctly (NOT SCREENSHOTS!!)

    use [ code ] [ / code ] tags (remove the extra spaces)
     
  12. ladman0311

    ladman0311

    Joined:
    Aug 16, 2012
    Posts:
    46
    Also, not sure if this is causing the issue, but the block in the image below are generated with a spawn gameObject. the script is turned on when the state is selected. then spawns the objects. the prefabs that are being generated are tagged correctly.

     
  13. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
    The issue is that you're ray casting, but you're not passing your raycasthit into the ray cast.

    Change line 108 to:

    Code (csharp):
    1. if(Physics.Raycast(ray, out hit, 10)){
     
  14. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    lol... so still hadnt made the one change that was needed... fail
     
  15. ladman0311

    ladman0311

    Joined:
    Aug 16, 2012
    Posts:
    46
    Ummm, let me see, i have used unity for about a week and i fail at the first script i write. How old are you?? Grow Up Dude!!!
     
  16. ladman0311

    ladman0311

    Joined:
    Aug 16, 2012
    Posts:
    46
    i will give that a try
     
  17. ladman0311

    ladman0311

    Joined:
    Aug 16, 2012
    Posts:
    46
    That fixed the null error, but my objects are still not being destroyed
     
  18. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    His reply wasn't about your Unity knowledge, it was a (rather rude imo) way of pointing out that the solution was given to you in the very first reply of this thread. If flaminghairball hadn't brought it up again, you may have been running in circles for a while :p

    Either way, fighting hostility with more hostility is never a way to get things done. Especially on the internet.

    EDIT:

    Are any of your logs firing? If not, add more logs so you can better understand what's happening (is the raycast hitting anything, if so what? etc)
     
    Last edited: Aug 28, 2012
  19. ladman0311

    ladman0311

    Joined:
    Aug 16, 2012
    Posts:
    46
    Your right, sorry to fight on a forum. i did try that on the first post and it did not work. so i must have fixed another error in my code that ended up making that work.

    I am doing that log thing right now actually. there are not flags going off, so i have a bug burred in the code somewhere.
     
  20. ladman0311

    ladman0311

    Joined:
    Aug 16, 2012
    Posts:
    46
    ok, so i have been trying to debug this for hours now, and i cant seem to find out why when the Ray hits my object that it does not destroy it when the args are met
     
  21. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,041
    Maybe its a good time to learn to use a debugger! Connect MonoDevelop to unity set a breakpoint in your code after the raycast hits, press play, and step through what is happening...
     
  22. unitrade

    unitrade

    Joined:
    Aug 27, 2012
    Posts:
    20
    try assigning Your RaycastHit object. it may probably solve a bit of this. have to compile it and see.