2
\$\begingroup\$

as I searched GameObject.Find and GameObject.FindwithTag are heavy to work with and for reall projects its better to find repalcements.

for a NPC that get instantiated in game it first needs to find player character. for this issue I only can use GameObject.find() .

for a scene with lots of objects maybe its heavy for even one frame if there is lots of objects in the scene. is there any way to set the researching object in head of hierarchi and the NPC to only get that?

\$\endgroup\$
4
\$\begingroup\$

The GameObject.Find() function sure is heavy, but it doesn't mean your game will run slow if you use it even once. The idea is that it's better to get reference of object in another way.

In your example, you can make a script that has static variables, one of them is a reference to the player. Once the player is initialised, it assigns itself to that variable, now every other object (even ones created dynamically) can reference the player from that variable.

That's not to say that GameObject.Find() should never be used, but when you are developing a game, it's not very comfortable thinking that every object you add makes everything run slightly slower. Especially if you only plan to run it in one frame, you'd have to have a big amount of objects to cause a noticeable delay.

Edit: Example

// MyGlobalConfig.cs
public class Config : MonoBehaviour {

    public static Transform player;
    /* Other static data */

}

// Player.cs
public class Player : MonoBehaviour {

    // ...

    void Start() {

        // Init player

        Config.player = transform;

    }

// SomeFile.cs
public class MyClass : MonoBehaviour {

    Transform player;
    // ...

    void Start() {

        player = Config.player;
        // ...

    }
}

You can of course save the player as something different than a Transform depending on your needs.

\$\endgroup\$
2
  • \$\begingroup\$ I tested it but I cant find out your meaning about static variable and reference. can you give an example? \$\endgroup\$
    – virtouso
    Apr 19 '18 at 8:47
  • \$\begingroup\$ @virtouso I edited my answer with an example \$\endgroup\$
    – TomTsagk
    Apr 19 '18 at 8:55
2
\$\begingroup\$

When you use Find only once when the scene loads, then performance is unlikely to be an issue. You would have to do that for quite a lot of objects to get a notable increase in scene load time.

But there are some other problems to keep in mind:

  • When you refer to objects by name, you can't rename that object. That might not be a problem in a simple project, but it can lead to problems in a more complex project. You will quickly lose track of which objects can be renamed and which can not because something refers to them by name.
  • Names are not enforced to be unique. When you have more than one object of the same name in the scene, you won't know which one you will find. It might find the correct one at first, but then suddenly it finds the other one, your game breaks, and you have no idea why.
  • When you refactor your project and turn the object into a prefab you instance at runtime, it will now have a (Clone) behind its name. I forgot that once and searched for that problem for hours.

So tl;dr: it's a very fragile solution which can easily break during future development.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.