Search Unity

Tile Palette Error On Dragging TIles

Discussion in '2D' started by asacafrw35tyth, Nov 22, 2018.

  1. asacafrw35tyth

    asacafrw35tyth

    Joined:
    Nov 17, 2015
    Posts:
    8
    ive reduced this error down to the smallest amount of dependencies possible. im in unity 2018.3.0b10. I can create a fresh project and import my script for creating tiles with prefabs attached. Then i can create a new "Prefab Tile" (the custom tile type i have that extends tilebase) , attach any sprite and an empty gameobject onto the new prefab tile asset, then drag it into the palette.

    This gives the error

    Setting the parent of a transform which resides in a Prefab is disabled to prevent data corruption.
    UnityEngine.GUIUtility: processEvent(Int32, IntPtr)

    while trying to find the cause of this bug, i have commented out all the extra functionality, and the same bug persists. I have traced it back to the line that assigns the prefab to tiledata.gameObject (line 64). If i comment out 64, the bug does not occur when i add the tile to the palette. with 64, it will always give the bug when dragging the tile into the palette.

    also, one other piece of relevant information, is that the StartUp method is getting called on the custom tile type when i attempt to add it to the palette in the editor mode. The docs say StartUp gets run on the first frame of runtime, so this seems like it could be causing the error, as the GameObject it would be trying to interact with would not be an instance yet, but a prefab.

    I thought downgrading versions fixed it. But, appropriately for the frustrating random feeling of this bug, it was actually not fixed. Downgrading versions made it work for a little bit (???). but it quickly returned to its old ways.

    Any suggestions or help is welcome.
    Thanks for your consideration
     
    Last edited: Nov 22, 2018
  2. asacafrw35tyth

    asacafrw35tyth

    Joined:
    Nov 17, 2015
    Posts:
    8
    forgot to add this because i was too overjoyed at the stupid error being fixed, but for completeness

    EDIT: error was not fixed. just magically came back.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Tilemaps;
    3. #if UNITY_EDITOR
    4. using UnityEditor;
    5. #endif
    6.  
    7. [CreateAssetMenu(fileName = "DefaultPrefabTile", menuName = "SpecialTiles/Prefab Tiles", order = 113)]
    8. public class PrefabTiles : AutoTile
    9. {
    10.     static int tileScale = 16;
    11.     public Sprite TileSprite;
    12.     public GameObject TileAssociatedPrefab;
    13.  
    14.     public float PrefabLocalOffset = 0;
    15.     public float prefabZOffset = 0;
    16.  
    17.  
    18.  
    19.     public override bool StartUp(Vector3Int position, ITilemap tilemap, GameObject go)
    20.     {
    21.  
    22.         //This prevents rogue prefab objects from appearing when the Tile palette is present
    23. #if UNITY_EDITOR
    24.         if (go != null)
    25.         {
    26.             if (go.scene.name == null)
    27.             {
    28.                 DestroyImmediate(go);
    29.             }
    30.         }
    31. #endif
    32.         if (go != null)
    33.         {
    34.             //Modify position of GO to match middle of Tile sprite
    35.             go.transform.position = new Vector3(tileScale*position.x + tileScale /2 + PrefabLocalOffset
    36.                 , tileScale* position.y + tileScale/2 +  PrefabLocalOffset
    37.                 , prefabZOffset);
    38.  
    39.         }
    40.  
    41.         return true;
    42.     }
    43.     /*
    44. #if UNITY_EDITOR
    45.     [MenuItem("Assets/Create/Prefab Tile")]
    46.     public static void CreatePrefabTiles()
    47.     {
    48.         string path = EditorUtility.SaveFilePanelInProject("Save Prefab Tile", "New Prefab Tile", "asset", "Save Prefab Tile", "Assets");
    49.  
    50.         if (path == "")
    51.             return;
    52.  
    53.         AssetDatabase.CreateAsset(ScriptableObject.CreateInstance<PrefabTiles>(), path);
    54.     }
    55. #endif
    56. */
    57.     public override void GetTileData(Vector3Int position, ITilemap tilemap, ref TileData tileData)
    58.     {
    59.         tileData.sprite = TileSprite;
    60.  
    61.  
    62.         if (TileAssociatedPrefab && tileData.gameObject == null)
    63.         {
    64.             tileData.gameObject = TileAssociatedPrefab;
    65.         }
    66.     }
    67. }
     
    Last edited: Nov 22, 2018