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

Tile Error

Discussion in 'Scripting' started by LutiArrais, Aug 12, 2019.

  1. LutiArrais

    LutiArrais

    Joined:
    Jun 27, 2017
    Posts:
    3
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Tilemaps;
    3. public class TreeTile : Tile
    4. {
    5.     public override bool StartUp(Vector3Int position, ITilemap tilemap, GameObject go)
    6.     {
    7.         go.GetComponent<SpriteRenderer>().sortingOrder = -position.y * 2;
    8.         return base.StartUp(position, tilemap, go);
    9.     }
    10. #if UNITY_EDITOR
    11.     [MenuItem("Assets/Create/Tiles/TreeTile")]
    12.     public static void CreateTreeTile()
    13.     {
    14.         string path = EditorUtility.SaveFilePanelInProject("Save TreeTile", "New TreeTile", "asset", "Save treetile", "Assets");
    15.         if (path == "")
    16.         {
    17.             return;
    18.         }
    19.         AssetDatabase.CreateAsset(ScriptableObject.CreateInstance<TreeTile>(), path);
    20.     }
    21. #endif
    22. }
    I coped that code from a video from youtube, and when I press play after stop, that I try to save it, appears 2 errors.

    ERROS:
    NullReferenceException: Object reference not set to an instance of an object
    TreeTile.StartUp (UnityEngine.Vector3Int position, UnityEngine.Tilemaps.ITilemap tilemap, UnityEngine.GameObject go) (at Assets/Scripts/TileScripts/TreeTile.cs:11)

    Error running StartUp for Tile.NullReferenceException: Object reference not set to an instance of an object
    TreeTile.StartUp (UnityEngine.Vector3Int position, UnityEngine.Tilemaps.ITilemap tilemap, UnityEngine.GameObject go) (at Assets/Scripts/TileScripts/TreeTile.cs:11)
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    We're missing some context here. Without a link to the Youtube video you're following, the best I can say is that either A) you missed some step the video wanted you to do, or B) the video may be using an obsolete version of Unity.

    Most likely the error is caused because the object being sent to you doesn't have a SpriteRenderer component, but without knowing anything about the rest of the system, I can't say why it doesn't have one (or possibly why the Youtube video thinks there SHOULD be one).
     
  3. LutiArrais

    LutiArrais

    Joined:
    Jun 27, 2017
    Posts:
    3
    < Here

    I Puti like that to verify if have SpriteRenderer:
    Doesnt work

    Code (CSharp):
    1.   public override bool StartUp(Vector3Int position, ITilemap tilemap, GameObject go)
    2.     {
    3.        if(go.GetComponent<SpriteRenderer>())
    4.         go.GetComponent<SpriteRenderer>().sortingOrder = -position.y * 2;
    5.         return base.StartUp(position, tilemap, go);
    6.     }
     
  4. Drugsnix

    Drugsnix

    Joined:
    Nov 22, 2017
    Posts:
    1
    Hey, i know its a late answer, but i too did follow that tutorial. The reason u get the errors as far as i understand it, is because it looks for the spriterendere on the object in the unity editor aswell, and not just in game. Hence because the objects get spawned in opun start of game, the objects and their spriterenderes doesent exists in the unity editor.
    i made a quick fix, and tested it abit..
    heres the fix:

    Code (CSharp):
    1.  public override bool StartUp(Vector3Int position, ITilemap tilemap, GameObject go) {
    2.  
    3.         if (go != null) {
    4.             go.GetComponent<SpriteRenderer>().sortingOrder = -position.y * 2;
    5.         }
    6.         return base.StartUp(position, tilemap, go);
    7.     }
    by checking if the objects exists before calling the spriterendere u get rid of the problem i explained over fix