Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 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 Reborn1214 · Jan 18, 2019 at 09:11 PM · unity 2dtilemap

How to use TileMap.GetInstantiatedObject?

I painted a GameObject on Tilemap with PrefabBrush.Now,I want get this GameObject.So, I use the function "TileMap.GetInstantiatedObject" and Debug it . But ,the result is Null.Here is My Code.

     private Grid grid;
     public GameObject Dirt;//drag to Inspector. the position is (-2.5,-1.5,0)
     public TileMap Background;//drag to Inspector
     
     private void Start()
     {
         grid = transform.GetComponent<Grid>();
         Vector3Int pos = grid.WorldToCell( Dirt.transform.position );
         Debug.Log(pos);//(-3,-2,0)
         Debug.Log( Background.GetInstantiatedObject(pos));//=> result is null
 
     }

Blockquote

  I also tried to change the Dirt's position to (-3,-2,0) In Inspector. But,It's still Null.


 
Comment
Add comment · Show 1
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 trapstreetgames · Jun 17, 2019 at 11:12 PM 0
Share

I don't feel quite knowledgeable enough to post a full-blown answer, but my experience has been that the object is instantiated after Start() runs on the parent. I suspect that in the next frame (maybe in Update()) you can call GetInstantiatedObject(pos) and get the result you're expecting.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by gubbi · May 29, 2020 at 07:00 PM

Late answer, but for others:

Gameobjects are instantiated some time after Start() runs, and before the first frame (before Update is first called). try GetInstantiatedObject from Update instead.

One dirty solution is to create an Init-method which runs on the first frame like this:

 private void Update() {
     if (!_inited) {
         _inited = true;
         Init();
     }
 }
 
 private void Init() {
    ...
    tilemap.GetInstantiatedObject(c3)
 }

This seems to be broken in 2019.4.0f1 though ...

Comment
Add comment · Show 1 · 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 AShenawy · Mar 29 at 09:57 AM 0
Share

Thanks for the tip! Indeed, this was bugging the heck out of me. Working on 2019.4.35 and can confirm that in Start() I get a null value for the game object. Instead of your solution, I used a coroutine which only needs to wait until the very first frame ends. Like so:

 private void Start()
 {
     StartCoroutine(InitTilemapObjects());
 }
 
 IEnumerator InitTilemapObjects()
 {
     // Wait a single frame for the tilemap to finish instantiating
     yield return new WaitForEndOfFrame();
     GameObject instObject = GroundTilemap.GetInstantiatedObject(new Vector3Int(0, 0, 0));
 }

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

104 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

Related Questions

2D tilemaps - separation of view and data, prototyping doubts 0 Answers

[Solved] Isometric Rule Tiles not Following Guidelines. 0 Answers

Remove a single tile from a tilemap 1 Answer

Best way to, change world tileset/tile pallet 0 Answers

"Random" Rule Tile 3 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