The question is answered, right answer was accepted
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);
}
}
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"):
Check your hierarchy if you didn't overlook something.
If it doesn't find it, it doesn't print:
Thanks for you comment Le Pampelmuse.
Those objects must exist then.
They don't... Here is screenshot:
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.
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:
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!
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!
Follow this Question
Related Questions
Why doesnt Gameobject.find not work? 3 Answers
GameObject.Find(CaseInsensetive) ? 1 Answer