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

Object reference not set to an instance of an object?

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

  1. iGolden

    iGolden

    Joined:
    Jan 24, 2015
    Posts:
    3
    Code (csharp):
    1. "NullReferenceException: Object reference not set to an instance of an object
    2. TileScript.SetTileSprite (UnityEngine.Transform settilesprite) (at Assets/Resources/Scripts/TileScript.cs:139)
    3. TileScript.Start () (at Assets/Resources/Scripts/TileScript.cs:91)"
    I've been stuck on a problem for a while which seems really simple to fix though I've tried various different ways around it to no use. I've tried looking this up but cannot find anything similar and any help would be great. The 18th line is causing the error (so it must be something to do with the function?). I have the following code;

    Code (csharp):
    1. for (int y = 2; y < mapHeight-2; y++) {
    2.       Transform tile = Instantiate (mountainTile, new Vector3 (Mathf.Round (y * tileSize), 2, 0), Quaternion.identity) as Transform;
    3.       tile.parent = transform;
    4.       map [2, y] = tile;
    5.       SetTileSprite(tile);
    6. }
    7.  
    8. Transform GetTileAt (int x, int y)
    9.     {
    10.         if (x < 0 || y < 0 || x > mapWidth || y > mapHeight)
    11.         {
    12.             Debug.LogWarning ("X or Y coordinate is out of bounds!");
    13.             return null;
    14.         }
    15.         return map[x, y];
    16.     }
    17.  
    18.     void SetTileSprite (Transform tile)
    19.     {
    20.         bool obj2right = false;
    21.         int x = Mathf.FloorToInt (tile.transform.position.x);
    22.         int y = Mathf.FloorToInt (tile.transform.position.y);
    23.         if (GetTileAt (x + 1, y).GetComponent<BoxCollider2D> () != null) {
    24.             obj2right = true;
    25.             Debug.LogWarning ("obj2right true");
    26.         }
     
  2. MarkFowler

    MarkFowler

    Joined:
    Nov 29, 2012
    Posts:
    31
    Is the tile a prefab?
     
  3. iGolden

    iGolden

    Joined:
    Jan 24, 2015
    Posts:
    3
  4. MarkFowler

    MarkFowler

    Joined:
    Nov 29, 2012
    Posts:
    31
    Try changing the tile to a GameObject, not a Transform.
     
  5. iGolden

    iGolden

    Joined:
    Jan 24, 2015
    Posts:
    3
    That gives me
    Code (csharp):
    1. Assets/Resources/Scripts/TileScript.cs(91,25): error CS1503: Argument `#1' cannot convert `UnityEngine.Transform' expression to type `UnityEngine.GameObject'
    (line 91 being line 2 in the bit I posted)

    If I change line 2 to GameObject then I get a bunch of errors, is that the only way I can do it?