talking between game objects?

ok so I’ve been using public variables/functions and using GameObject.Find(); or GameObject.FindGameObjectWithTag(); to transfer variable data between gameobjects.

I was just wondering, is there a better, more optimized way?

So far I don’t think my method will be that bad because it’s all done based on user interactivity, instead of being inside the Update(); function, so the Find(); function(s) are only called when the user does a certain action. however, I’d like to learn a better way if there is one, as this way feels kind of sloppy to me.

I basically have been using the public static method and it is working great- then you just put the class.whaterver it is you want-

Well Akinon93, it depends on how many objects you will be interacting with. You are doing the right thing if you are not calling Find() inside your Update functions since they are costly. If you plan in communicating with the same object (Or the same collection of objects) throughout the section, you could call all the finds in the Start function and assign the results to local variables, which can be easily accessed later. I do a lot of communication between gameobjects of different types. Keep in mind that this type optimization could be done if you know ahead of time what objects you well be accessing.

If you need to communicate to the same object over and over from different objects in the scene, here is a technique I use so I don’t have to call “Find” at all.

Many games usually have one main character moving around. (Of course this depend on your game). So I usually have my main gameobject contain a set of public static functions that allow me access to certain information. If you do this, though, you have to be carful on how you store a static private link to the instance you want to comunicate with. Here is an example of what I would do in such cases. Say I have a class called hero and I need to read information from him, like position or health. Notice this only works if there is only one Hero per scene.

// Example in C#

public class Hero : MonoBehaviour
{
    // Here we have all the static section
    private static Hero currentHeroLink;

    // Access the position of the hero
    public static Vector3 Position
    {
        get
        {
            if(hero != null)
            {
                return hero.transform.position;
            }
            else
            {
                Debug.LogError("We should not be accessing his if hero is not initialized");
            }
        }
    }
    public static int getHealth()
    {
        get
        { 
            if(hero != null)
            {
                return hero.health;
            }
            else
            {
                Debug.LogError("We should not be accessing his if hero is not initialized");
            }
        }
    }

    // non-static section of the class. Here you put all the stuff you want to handle within the game object
    
    private int health = 10;
    // It is important to initialize hero every time you go through the Awake function, so that this link
    // is update for every new instance of the Hero Class
    void Awake()
    {
        Hero.hero = this; 
    }
   
}

By doing this I access information easily from any class external to the Hero class without doing a Find(“Hero”) on every class that needs to access the hero.
Just as a side note, if you are not familiar with static vs local methods, read this http://www.dotnetperls.com/static-method

I hope this help you at least a little :slight_smile: