Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Khena_B · Aug 03, 2018 at 08:54 AM · variabletilemapscriptableobjecttilepublic

Scriptable tile

Trying to follow some tutorials and create a scriptable tile for the Tilemap system, I created a script with some public variables, when I use GetTile() I manage to get a reference to the scriptable tile, but I'm unable to access those public variable, why can't I access the three public variables in this script?

 #if UNITY_EDITOR
 using UnityEditor;
 #endif
 
 namespace UnityEngine.Tilemaps
 {
     public enum GroundType { Grass, GrassWet, Metal, Wood, Sand}
 
     public class GroundTile : Tile
     {
         public Sprite tileSprite;
         public GroundType groundType;
         public int layer;
 
         public override void GetTileData(Vector3Int location, ITilemap tileMap, ref TileData tileData)
         {
             base.GetTileData(location, tileMap, ref tileData);
             {
                 tileData.sprite = tileSprite;
                 tileData.colliderType = Tile.ColliderType.Sprite;
             }
         }
 
 #if UNITY_EDITOR
         [MenuItem("Assets/Create/Ground Tile")]
         public static void CreateGroundTile()
         {
             string path = EditorUtility.SaveFilePanelInProject("Save Ground Tile", "New Ground Tile", "asset", "Save Ground Tile", "Assets");
             if (path == "")
                 return;
             AssetDatabase.CreateAsset(ScriptableObject.CreateInstance<GroundTile>(), path);
         }
 #endif
     }
 
 #if UNITY_EDITOR
     [CustomEditor(typeof(GroundTile))]
     public class GroundTileEditor : Editor
     {
         public GroundTile tile { get { return (target as GroundTile); } }
 
         public override void OnInspectorGUI()
         {
             EditorGUI.BeginChangeCheck();
             tile.tileSprite = (Sprite)EditorGUILayout.ObjectField("Sprite", tile.tileSprite, typeof(Sprite), false);
             tile.groundType = (GroundType)EditorGUILayout.EnumPopup("Ground Type", tile.groundType);
             tile.layer = EditorGUILayout.LayerField("Layer", tile.layer);
             if (EditorGUI.EndChangeCheck())
                 EditorUtility.SetDirty(tile);
         }
 #endif
 }


This prints: MyTile (UnityEngine.Tilemaps.GroundTile)

 Tilemap tileMap = collider.GetComponent<Tilemap>();
 TileBase currentTile = tileMap.GetTile(tileMap.WorldToCell(collisionPos));
 print(currentTile);

Now that I have a reference to my custom tile, how do I get those public variables?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by cryingwolf85 · Aug 03, 2018 at 04:20 PM

Instead of TileBase currentTile = tileMap.GetTile(tileMap.WorldToCell(collisionPos));

Try this: GroundTile currentTile = tileMap.GetTile(tileMap.WorldToCell(collisionPos)) as GroundTile;

If you have more than one type of tile, and you're not sure of the tile type at the time, you can do something like this:

         TileBase currentTile = tileMap.GetTile(tileMap.WorldToCell(collisionPos));
 
         if (currentTile is GroundTile) {
             // Do stuff for a ground tile
         }
         
         else if (currentTile is WaterTile) {
             // Do stuff for a water tile
         }
         
         // .. etc

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Khena_B · Aug 03, 2018 at 04:22 PM 0
Share

Thank you so much, tried that earlier but was missing "as GroundTile", it works!

avatar image cryingwolf85 Khena_B · Aug 03, 2018 at 04:23 PM 0
Share

Great! Glad I could help.

avatar image Rokugan90 · Aug 23, 2019 at 05:51 PM 0
Share

You sir are the $$anonymous$$VP! Was banging my head for hours against this

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

98 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Need tiles to contain data or a reference 0 Answers

How can I modify the sprite of a particular Scriptable Tile in a Tilemap? 2 Answers

Why can't I make this VAR public ? (Solved) 3 Answers

Custom tiles for tilemap problem 0 Answers

2D tiles, which is the best approach? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges