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 /
  • Help Room /
This question was closed May 19, 2016 at 12:58 PM by Le-Pampelmuse for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by xpavelos · May 17, 2016 at 08:57 PM · gameobject.find

GameObject.Find("object name") never returns null. Even if object doesn't exist!

Okay, this is very strange... I am using GameObject.Find() in editor(not runtime) and whenever I call it - it returns object, even if it doesn't exist.

 GameObject checkTile = GameObject.Find(tileMap.name + "/Tiles/tile "+ brushPosX + "x" + brushPosY);
         if (checkTile !=null)
         {
             Debug.Log(checkTile);
         }

And console shows: "tile 1x4" or "tile 33x76" or tile "4x15", etc. But there are no such objects!

I also tried this:

 if (GameObject.Find(tileMap.name + "/Tiles/tile "+ brushPosX + "x" + brushPosY))
 {
     GameObject checkTile = GameObject.Find(tileMap.name + "/Tiles/tile "+ brushPosX + "x" + brushPosY);
 
     if (checkTile !=null)
     {
         Debug.Log(checkTile);
     }
 }

Comment
Add comment · Show 5
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 Le-Pampelmuse · May 17, 2016 at 10:14 PM 2
Share

Those objects must exist then.

I copied your script:

 using UnityEngine;
 using System.Collections;
 
 public class a : $$anonymous$$onoBehaviour {
 
     Transform tile$$anonymous$$ap;
     int brushPosX = 2, brushPosY = 3;
 
     void Start () {
         tile$$anonymous$$ap = transform;
 
         GameObject checkTile = GameObject.Find(tile$$anonymous$$ap.name + "/Tiles/tile"+ brushPosX + "x" + brushPosY);
         if (checkTile !=null)
         {
             Debug.Log(checkTile);
         }
     }
 }

And it gives me a correct answer (only showing "tile2x3"): alt text

Check your hierarchy if you didn't overlook something.

If it doesn't find it, it doesn't print: alt text

working.png (15.3 kB)
correct.png (11.4 kB)
avatar image xpavelos · May 18, 2016 at 09:27 AM 0
Share

Thanks for you comment Le Pampelmuse.

Those objects must exist then.

They don't... Here is screenshot:

alt text

ua.png (38.0 kB)
avatar image Le-Pampelmuse xpavelos · May 18, 2016 at 08:06 PM 2
Share

That's strange, I need more information about your script(s) then.

What happens when you use my class "a" ins$$anonymous$$d of yours on the GameObject Tilemap?

What is "tile$$anonymous$$ap" in your project? A Transform, right? Anything special to it?
I just used the transform where the script is applied to.

Is your script doing something else besides that? Please post the script so we can maybe spot the error together.

What is and what does it do?

PS: Do the tile objects exist before you hit play? If they are destroyed at runtime when starting, your "checking" script could execute before that happens, so it prints their name but they are destroyed a few frames later. which is impossible to spot by looking at the hierarchy.

avatar image xpavelos · May 18, 2016 at 10:23 PM 0
Share

Thanks again for your interest. The tilemap handles all tile creation, displays grid and is used as custom inspector.

I can't post whole script, because there is 3000 characters limit, but here is troubling part:

     void RemoveTile(int x, int y, bool updateNeighbours)
     {
         GameObject tilesRoot = GameObject.Find(tile$$anonymous$$ap.name + "/Tiles");
         if (tilesRoot != null)
         {
             GameObject checkTile = GameObject.Find(tile$$anonymous$$ap.name + "/Tiles/tile "+ x + "x" + y);
             if (checkTile)
             {
                 Debug.Log(checkTile);    // Here is happening paranormal thing... ;p
                 if (checkTile !=null)
                 {
 
                     DestroyImmediate(checkTile);
                     if (updateNeighbours) UpdateTileNeighbours(x, y);
                 }
             }
         }
     }

And screenshot: alt text

tilemap.png (244.7 kB)
avatar image xpavelos · May 18, 2016 at 11:02 PM 0
Share

The Debug.Log is not called during runtime, its all happening in the editor. But your suggestion led me to thinking that I may create and remove them in the same frame or something like that. I will have a look at this tomorrow and let you know. Thanks!

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by xpavelos · May 19, 2016 at 09:09 AM

Stupid me. The way my editor works is when left mouse button is held down a tile is drawn and when shift and mouse button is held down - the tile gets removed. When I was painting tiles I used this condition in OnSceneGUI():

 if (curE.type == EventType.MouseDrag && curE.button == 0 ||
     curE.type == EventType.MouseDown && curE.button == 0)    
     {
         if (toolIndex == 0)
         {
             if (tileMap.floorSet != null) DrawAutoTile();
         } else if (toolIndex == 1)
         {
             //... irrelevant
         }
     }

So basically what I did wrong is I allowed painting tiles even when shift key was down. And this fixed the problem:

 Event curE = Event.current;
 if (curE.shift) shiftDown = true;
 else shiftDown = false;
 
 if (curE.type == EventType.MouseDrag && curE.button == 0 && !shiftDown||
     curE.type == EventType.MouseDown && curE.button == 0 && !shiftDown)
 {
     if (toolIndex == 0)
     {
         if (tileMap.floorSet != null) DrawAutoTile();
     } else if (toolIndex == 1)
     {
         // ... Irrelevant
     }
 }

Thanks again Le Pampelmuse for your support!

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 Le-Pampelmuse · May 19, 2016 at 12:58 PM 1
Share

Better late than never! ;)

Follow this Question

Answers Answers and Comments

42 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

Related Questions

I want to find a specific object and while on hover (using raycast) change the color but I cant seem to get this right 0 Answers

How to translate Player's position to another GameObject's position using raycast. Help me please with my script 0 Answers

Why doesnt Gameobject.find not work? 3 Answers

GameObject.Find(CaseInsensetive) ? 1 Answer

How do I find other gameObject and disable it? 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