Problems with the .Find methods

Hello everyone. :slight_smile:
I don’t know if this is the right place for this kind of question so I apologize if it isn’t

I have this Hierarchy, as in the attached file

Main Camera
LevelManager
Map
-sea(Clone)
–HQ(Clone)
-sea(Clone)
-sea(Clone)

Anyone knows which line of code will return me the HQ(Clone) and frome where I have to call it?

I don’t want to waste your time, but I’ve tried everything with GameObject.Find and transform.Find and both always returns null. Maybe I’m calling them in the wrong place or with the wrong string. Only GameObject.FindGameObjectWithTag seems to work, but is not what I’m insterested in.

Thx!

3075953--231445--Transform.png

This should work in a Start function (HQ game object needs to be active)

GameObject gameObjHQ = GameObject.Find("HQ(Clone)");
Debug.Log(gameObjHQ.name);

This goes down the tree so needs a referance from one of the parent tranforms.

Transform transformHQ = mapTransform.Find("HQ(Clone)");
Debug.Log(transformHQ.name);
2 Likes

If you’re unable to Find(“HQ(Clone)”), that’s because nothing named that exists. Since it’s named “Clone”, I assume you’re instantiating it at runtime?

It could be that you’re trying to find it before you’ve Instantiated it.

1 Like

Thank you mate, The code you gave me works as expected. :slight_smile:
Any idea about how can I do the same with Transform.Find(because I have to be sure that i’m pointing towards the child of the specific sea I’m looking at)

[quote=Baste, post: 3076095, m
ember: 185905]
If you’re unable to Find(“HQ(Clone)”), that’s because nothing named that exists. Since it’s named “Clone”, I assume you’re instantiating it at runtime?
It could be that you’re trying to find it before you’ve Instantiated it.
[/quote]

Good Question. :slight_smile: I don’t know if this is the answer but I’ve also tried to use the .name function to get a string with the actual name of the objects. Also, HQ is created from the Start function and the experiments I’ve tried to do were all in the Update Function

Since you’re creating it from script, you can just store the reference when you create it instead of Finding it.

Find is incredibly slow, as it searches through your entire hierarchy for anything with the name. Having it in Update will have a pretty major performance impact.

If you’re trying to Find it from the same script that’s spawning it, just store it in a variable there. If you’re Finding it from a different script, you can assign that in the inspector of the spawning script and send it along that way.

Explain what you’re trying to do, and it’ll be easier to give you advice!

1 Like

if you already have the sea transform do…

Transform transformHQ = seaTransform.Find("HQ(Clone)");
Debug.Log(transformHQ.name);

or from the sea GameObject.

Transform transformHQ = seaGameObj.transform.Find("HQ(Clone)");
Debug.Log(transformHQ.name);

This should return the HQ(Clone) below the the seaTransform/gameobject
.
Take note of Baste advice, only use find in run once type code (Start, onEvent type code). Dont use them in update and fixedupdate unless you know its only going to to run once.

1 Like

Thank you! :slight_smile: Now I have an idea about how to implement what I have in mind :wink:

Thank you! I really appreciate performace-related suggestions. It’s not easy to find them out of such discussions. :slight_smile:
As I said I’ve put the .Find functions in Update only for testing them. In my project I’ve created a cursor which moves on a board, and only when is in the same square of a building I will reference that building using the .Find from the square it is on(squares I have already stored in a Dictionary inside a Singleton, so very easy to access), so it will be only a single time everytime the player decide to select that building. You think it would be accettable?
Ah I forgot to mention, is a very light 2D game(without any collider and such).

If I’ve not been clear just let me know. :slight_smile: I’m not a native english speaker.

It will be fine when related to user input events. You just dont want them running at 60FPS.

1 Like

Excellent! :slight_smile:

By the way, I’ve tried the code you gave me about the transform.Find and it does seem to work perfectly! :slight_smile:
Another quick question, always performance related(not off-topic because it will be the consequence of the implementation of the transform.Find in my project).

I’ve elaborated some sort of AStar algorythm for my project, which will take into account some variables to pathfind through the board(I will not go deeply into details, but it will use a stack to store the optimal path etc.).
Can the Astar be considered efficent in it’s own way?

A* is a very popular pathfinding algorithm. It does, however, not guarantee that the shortest path is found, only that (in layman’s terms) a short path is found fast. It’s used in the back-end for most popular pathfinding solutions.

It’s a bit complex to implement. If you want a pre-built pathfinding solution, the A* Pathfinding Project is pretty good, as it gives you a pretty flexible working base. The free version lacks some features, but should be a lot easier to start out with than building things from scratch.

About GameObject.Find, as you learn to use the engine you should try to move away from it. It’s not only a bad idea for performance reasons. Explanation. For starting out, it’s not a big problem, though.

1 Like

Thank you but I’m not new to this kind of things and I know already how to implement it. :slight_smile:
Mine in particular is not a true A*, in the sense that it uses a similar evaluation behavior but I will use it to create a tree-list of the possible moves in the game, since it will give the choice of movement to the player and will take into account the terrain your passing on.
As for the logic itself, I’ve already created some boardgames like chess, in which I used some complex mathematical solution to move the pieces.

Well sorry, I don’t want to annoy you with a ton of details. :slight_smile:
I’m just trying to know in advance an algorithm based(only theoretically on the A*) could be considered an efficent solution(albeit not the best in terms of quality of the results) for a 2D light-weight game on the Unity engine, and since I’ve been blessed with two skilled programmers in the discussion I’m just taking advantage of it. :wink:

I know it may seem strange that someone already fonded with the A* was stuck on the transform.Find like an idiot, but it’s just that Unity is a world I’m exploring with this project for the first time :stuck_out_tongue:

As always, sorry if I’ve misspelled something