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. Dismiss Notice

Null Reference exepction in custom class function

Discussion in 'Scripting' started by Dev_Dmajster, May 5, 2014.

  1. Dev_Dmajster

    Dev_Dmajster

    Joined:
    Apr 14, 2014
    Posts:
    18
    Hello!

    i made an array of objects called MapInstances, it contains 400 gameobjects, and now i want to isolate some of them by using my custom class function called "Instance.Find", here is the script

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Instance
    6. {
    7.     public static GameObject Find(int X,int Y)
    8.     {
    9.         int MapWidth = GameObject.Find("obj_MapReader").GetComponent<MapReader>().MapWidth;
    10.         int MapHeight = GameObject.Find("obj_MapReader").GetComponent<MapReader>().MapHeight;
    11.  
    12.         GameObject WantedInstance = GameObject.Find("obj_MapReader").GetComponent<MapReader>().MapInstances[ X + (Y * MapWidth) ].gameObject;
    13.  
    14.         return WantedInstance;
    15.     }
    16. }
    17.  
    when i try to use it, it gives me a null reference exepction, why is this happening?
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Does it tell you anything about the line the error occurs?

    1) Make sure your gameobject which you are trying to find is called exactly 'obj_MapReader'
    2) Make sure the MapReader is attached to this gameObject called obj_MapReader
    3) is your array already filled?
    4) do you try to just find and return the object by now or do you have any other code working at the same time or being executed right after that which might cause an exception?
     
  3. Dev_Dmajster

    Dev_Dmajster

    Joined:
    Apr 14, 2014
    Posts:
    18
    This is the error message

    Code (csharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. Instance.Find (Int32 X, Int32 Y) (at Assets/SCRIPTS/UsefulFunctions.cs:10)
    3. MapReader.Start () (at Assets/SCRIPTS/MapReader.cs:119)
    1) i checked 2x it is the same ( uppercase and lowercase )
    2) MapReader is a script used by obj_MapReader
    3) If you mean the MapInstances array it is filled yes.
    4) im just trying to return the object, and i haven't got any code executed after or in the same time while this script is running ( well i do, but it's just a bunch of mesh deformation scipts i use)
     
    Last edited: May 5, 2014
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    I've copied the script and created the ones which were necessary in order to make it work and it worked perfectly fine.
    What code do you have in MapReader in and around line 119?
     
  5. Dev_Dmajster

    Dev_Dmajster

    Joined:
    Apr 14, 2014
    Posts:
    18
    Instance.Find(20,20);
     
  6. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    And what's your MapWidth, the value at the time the script tries to execute that line?
     
  7. Dev_Dmajster

    Dev_Dmajster

    Joined:
    Apr 14, 2014
    Posts:
    18
    well this is my map reader script, by my logic it should aready be 20 at the time it gets to instance find.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4. using System.IO;
    5. using System.Collections.Generic;
    6.  
    7.  
    8. public class MapReader: MonoBehaviour  
    9. {
    10.     public string MapName = "test.map";
    11.     public int MapWidth = 0;
    12.     public int MapHeight = 0;
    13.  
    14.     private int ArrayX = 0;
    15.     private int ArrayZ = 0;
    16.  
    17.     public GameObject GameObject_DirtWall;
    18.  
    19.    
    20.     public GameObject[] MapInstances;
    21.  
    22.  
    23.     public string[] gameobjectarray1;
    24.     public string[] gameobjectarraytemp;
    25.  
    26.  
    27.     public string Status = "NONE";
    28.  
    29.     void Start ()  
    30.     {
    31.         string MapInfo = "Maps/" + MapName;
    32.         string MapInfoData= System.IO.File.ReadAllText (MapInfo);
    33.        
    34.         gameobjectarray1 = MapInfoData.Split ( "\n"[0]);
    35.  
    36.  
    37.  
    38.  
    39.  
    40.         for(int i = 0 ; i < gameobjectarray1.Length ; i++ )
    41.         {
    42.             gameobjectarraytemp = gameobjectarray1[i].Split(","[0]);
    43.  
    44.             if(gameobjectarraytemp.Length == 1)
    45.             {
    46.                 if(Status == "NONE")
    47.                 {
    48.                     if( gameobjectarraytemp[0].Contains("MAPSIZE") == true || gameobjectarraytemp[0].Contains("MAPINFO") == true )
    49.                     {
    50.                         Status = gameobjectarraytemp[0];
    51.                         Debug.Log("Changed status to: " + Status);
    52.                     }
    53.                     else
    54.                     {
    55.                         Debug.LogError("TRIED TO SET STATUS TO" + gameobjectarraytemp[0]);
    56.                     }
    57.                 }
    58.                 else if(Status != "NONE")
    59.                 {
    60.                     if(gameobjectarraytemp[0].Contains("END") == true)
    61.                     {
    62.                         Status = "NONE";
    63.                         Debug.Log("Status ended!");
    64.                     }
    65.                     else
    66.                     {
    67.                         Debug.LogError(gameobjectarraytemp[0]);
    68.                     }
    69.                 }
    70.  
    71.  
    72.             }
    73.             else
    74.             {
    75.                 for(int i2 = 0; i2 < gameobjectarraytemp.Length ; i2 ++)
    76.                 {
    77.                     if(Status.Contains("MAPSIZE") == true)
    78.                     {
    79.                         if( MapWidth == 0)
    80.                         {
    81.                             Debug.Log("Map width is: "+gameobjectarraytemp[i2]);
    82.                             MapWidth = Convert.ToInt32( gameobjectarraytemp[i2] );
    83.                         }
    84.                         else if (MapHeight == 0)
    85.                         {
    86.                             Debug.Log("Map height is: "+gameobjectarraytemp[i2]);
    87.                             MapHeight = Convert.ToInt32( gameobjectarraytemp[i2] );
    88.                             MapInstances = new GameObject[(MapWidth*MapHeight)+1];
    89.                         }
    90.                         else
    91.                         {
    92.                             Debug.LogError("Too many parameters for map size");
    93.                         }
    94.  
    95.                     }
    96.                     else if(Status.Contains("MAPINFO") == true)
    97.                     {
    98.                         if (gameobjectarraytemp[i2].Contains("x") == true)
    99.                         {
    100.                             ArrayX++;
    101.                             Debug.Log(ArrayX+(MapWidth*ArrayZ));
    102.                             GameObject newObject = GameObject.Instantiate(GameObject_DirtWall,new Vector3(ArrayX,0,ArrayZ+1),transform.rotation)as GameObject;
    103.                             MapInstances[ArrayX+(MapWidth*ArrayZ)] = newObject;
    104.  
    105.  
    106.  
    107.  
    108.                             if (ArrayX == MapWidth  ArrayZ < MapHeight-1)
    109.                             {
    110.                                 ArrayX = 0;
    111.                                 ArrayZ++;
    112.                             }
    113.                         }
    114.                     }
    115.                 }
    116.  
    117.             }
    118.         }
    119.         Instance.Find(20,20);
    120.     }
    121. }
     
  8. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    20 +(20*20) = 420.. if you only have 400 objects in that array that won't work. But usually Unity would show an index out of range error then.
     
  9. Dev_Dmajster

    Dev_Dmajster

    Joined:
    Apr 14, 2014
    Posts:
    18
    i don't seem to get an out of range error, only the null reference one and still i have no idea why is that

    this is my standard map save format, if you want to test it out on your project make a notepad file called

    "test.map" and put this in it
    Code (csharp):
    1.  
    2. MAPSIZE
    3. 20,20
    4. END
    5.  
    6. MAPINFO
    7. x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x
    8. x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x
    9. x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x
    10. x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x
    11. x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x
    12. x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x
    13. x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x
    14. x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x
    15. x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x
    16. x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x
    17. x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x
    18. x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x
    19. x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x
    20. x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x
    21. x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x
    22. x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x
    23. x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x
    24. x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x
    25. x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x
    26. x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x
    27. END
    28.  
    then save it into your project folder in a newly created /Maps folder
     
    Last edited: May 5, 2014
  10. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    I think i might have found something but let me check that first.
     
  11. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Well i get an index out of range exception. :S
    But i also get the message which you've coded in with "too many parameters for map size" and "tried to set status to ".

    Might also be caused by coping the file from that codebox as there're always tabs etc... gonna try again in a bit.
     
  12. Dev_Dmajster

    Dev_Dmajster

    Joined:
    Apr 14, 2014
    Posts:
    18
    hmm, i also noticed that in the actual code i haven't set the begining status to "NONE" , that could be the reason you're getting all the errors
     
  13. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Ok, i it works now... i probably changed the MapWidth in inspector by accident when i set an gameObject to the dirty variable so it could never get into the MapWidth == 0 condition. Haha.

    Now, it works fine, the only thing that doesn't work now is the line i told you about which is out of range.

    Code (csharp):
    1.  
    2. Instance.Find(20,20);
    3.  
     
  14. Dev_Dmajster

    Dev_Dmajster

    Joined:
    Apr 14, 2014
    Posts:
    18
    i can't seem to fix the problem ... hmm it can't be out of range, because it creats only as many of objects as there is x's in the test.map file, i checked and it is 20x20 so it always creates only 400 gameobjects
     
    Last edited: May 5, 2014
  15. Abu-Faisal

    Abu-Faisal

    Joined:
    Sep 11, 2013
    Posts:
    15
    MapInstance[x] is a gameobject, so MapInstance[x].gameObject isn't right I think.
     
  16. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Have you assigned an object to the GameObject_DirtWall object?
     
  17. Dev_Dmajster

    Dev_Dmajster

    Joined:
    Apr 14, 2014
    Posts:
    18
    hmm yes i just noticed that they're stored directly as gameobjects ,but still even after i remove the .gameobject suffix the problem persists
     
    Last edited: May 5, 2014
  18. Dev_Dmajster

    Dev_Dmajster

    Joined:
    Apr 14, 2014
    Posts:
    18
    Yes i have, all the objects are created (20x20 square with a total of 400 objects) and i have 401 array entries ( i made the 0 empty for easier use of my scripts).
     
    Last edited: May 5, 2014
  19. Abu-Faisal

    Abu-Faisal

    Joined:
    Sep 11, 2013
    Posts:
    15
    Just a long shot.
    Try to drop WantedInstance in your Find function and directly return the right side.

    As I can see the error it is static function error means your tring to do some variables that isn't belong to any instance(object). I'm not expert in coding but try it.
     
  20. Dev_Dmajster

    Dev_Dmajster

    Joined:
    Apr 14, 2014
    Posts:
    18
    i tried it with the following code

    Code (csharp):
    1. return GameObject.Find("obj_MapReader").GetComponent<MapReader>().MapInstances[ X + (Y * MapWidth) ];
    and
    Code (csharp):
    1.  
    2. Instance.find(1,1);
    but it still throws the same error
     
  21. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    That's really strange... as i said your script works, and the only thing i can suggest again is checking whether the script is attached to to correct object and whether GameObject_DirtWall is set to an gameobject in the inspector... if so, screen your setup and upload the screenshots
     
  22. Dev_Dmajster

    Dev_Dmajster

    Joined:
    Apr 14, 2014
    Posts:
    18
    still not working, if you would be willing to take a look here is my dropbox

    Link removed
     
    Last edited: May 6, 2014
  23. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Break up your huge compound statement into multiple lines so you can figure out exactly *what* is null.
     
  24. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    First post =>

    Your object in the scene is called MapReader, not obj_MapReader.

    Change either the GameObject's name or the string you are using in order to find that GameObject..
     
  25. Dev_Dmajster

    Dev_Dmajster

    Joined:
    Apr 14, 2014
    Posts:
    18
    Oh thank you very much ! it seems i have reverted to my previous project backup and forgot to change that ! thank you a million times ! 1000+ cookies for you :D