Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Jack-Howard · Feb 25, 2020 at 03:56 AM · tilemapinheritancetiletilescustom class

How can I get my Custom Tile from my tileMap

I have created a custom tile that inherits from Tile. I need my tiles to store several variables and thought this would be the best way:

     using UnityEngine;
     using UnityEngine.Tilemaps;
     [CreateAssetMenu(fileName = "New AStarTile", menuName = "Tiles/AStarTile")]
     public class AStarTile : Tile
     {
         public TileScriptableObject tileScriptableObject;
         public Sprite litSide;//Tile image that should be shown when lit
         public Sprite darkSide;//Tile image that should be shown when dark
         public bool currentlyLit = false;//Whether the tile has been flipped
     }

I want to get one of these custom AStarTiles from my tileMap and change its sprite from its stored darkSide sprite, to its litSide sprite. Focus on the ChangeTile function, at the bottom.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Tilemaps;
 
 public enum TileType {ENTRANCE, EVENT, AMBUSH, CRYSTAL, TREASUREROOM }
 
 public class Astar : MonoBehaviour
 {
     private TileType tileType;
 
     [SerializeField]
     private Tilemap tileMap;
 
     [SerializeField]
     private AStarTile[] tiles;
 
     //[SerializeField]
     //private RuleTile entrance;
 
     [SerializeField]
     private Camera camera;
 
     [SerializeField]
     private LayerMask layerMask;
 
     private TileScriptableObject tileScriptableObject;//current tiles scriptable object
 
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             RaycastHit2D hit = Physics2D.Raycast(camera.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, Mathf.Infinity, layerMask);
 
             //if (hit.collider == null)
             //{
                 Vector3 mouseWorldPos = camera.ScreenToWorldPoint(Input.mousePosition);
                 Vector3Int clickPos = tileMap.WorldToCell(mouseWorldPos);
                 ChangeTile(clickPos);
            // }
         }
     }
 
     private void ChangeTile(Vector3Int clickPos)//used to flip tile to other side
     {
          AStarTile selectedTile = tileMap.GetTile(clickPos);
 
 
         if (selectedTile.currentlyLit == false)//if the tile is not lit
         {
             selectedTile.currentlyLit = true;
             selectedTile.SetSprite(selectedTile.litSide);
 
             //tileMap.SetTile(clickPos, tiles[0]);
         }
 
     }
 }

The issue I'm having is this line:

  AStarTile selectedTile = tileMap.GetTile(clickPos);

I'm getting the warning: "Assets\Scripts\Astar.cs(48,35): error CS0266: Cannot implicitly convert type 'UnityEngine.Tilemaps.TileBase' to 'AStarTile'. An explicit conversion exists (are you missing a cast?)"

This doesnt make sense to me because my tile inherits from the Tile class. If i change the line to:

  TileBase selectedTile = tileMap.GetTile(clickPos);

Then it doesn't give me a warning but I cant get the custom variables i need.

If you know how I can fix this line or if you know a better method I could be doing this then I would greatly appreciate it. Thanks.

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 ThisIsDangerous · Feb 25, 2020 at 04:45 AM

The error is telling you that you are missing a cast. Do the cast.

AStarTile selectedTile = (AStarTile) tileMap.GetTile(clickPos);

You probably want to do it more safely.

var tile = tileMap.GetTile(clickPos);
if(tile is AStarTile) {
   var selectedTile = (AStarTile) tile;
   // more code
}

Comment
Add comment · Show 4 · 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 Jack-Howard · Feb 26, 2020 at 03:31 AM 0
Share

Thank you. That worked perfectly. Could you help me with one more thing please? Im trying to change the tiles sprite from the Astar script. I have added this function in the AStarTile script public void ChangeTile(Vector3Int position, ITilemap tilemap, ref TileData tileData, Sprite newSprite) { tileData.sprite = newSprite; tilemap.RefreshTile(position); }

Then I am calling the function from my Astar script with this line: selectedTile.ChangeTile(clickPos, tile$$anonymous$$ap, , selectedTile.litSide);

How do I get the "ref TileData tileData"? I dont understand what to put in the 3rd slot for ChangeTile. Thanks

avatar image ThisIsDangerous · Feb 26, 2020 at 04:37 AM 0
Share

Actually I haven't had a chance to work with scriptable tiles and don't know how exactly it should be done. But since you want to change the rendered tile sprite, I'd just try and call selectedTile.sprite = yourNewSprite; probably this will work as you intend.

avatar image ThisIsDangerous · Feb 26, 2020 at 04:46 AM 0
Share

So in your example it should look something like this:

if (selectedTile.currentlyLit == false)
{
    selectedTile.currentlyLit = true;
    selectedTile.sprite(selectedTile.litSide);
}

avatar image Jack-Howard ThisIsDangerous · Feb 26, 2020 at 07:58 AM 0
Share

It doesn't let me do that unfortunately. I get the error "Non-invocable member 'Tile.sprite' cannot be used like a method". But thanks for trying, and for answering my initial question.

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

128 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 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

How to get adjcent tiles (horizontal or vertical) in an isometric tilemap ? 1 Answer

Remove a single tile from a tilemap 1 Answer

Unity Tilemap lost all tiles 0 Answers

Having trouble with dragging sprites into the tile palette 1 Answer

Tilemaps making feature 1 Answer


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