Search Unity

talking between game objects?

Discussion in 'Scripting' started by Akinon93, Oct 30, 2011.

  1. Akinon93

    Akinon93

    Joined:
    Jan 13, 2011
    Posts:
    185
    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.
     
  2. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    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-
     
  3. jfeliu

    jfeliu

    Joined:
    Apr 27, 2011
    Posts:
    24
    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.

    Code (csharp):
    1.  
    2. // Example in C#
    3.  
    4. public class Hero : MonoBehaviour
    5. {
    6.     // Here we have all the static section
    7.     private static Hero currentHeroLink;
    8.  
    9.     // Access the position of the hero
    10.     public static Vector3 Position
    11.     {
    12.         get
    13.         {
    14.             if(hero != null)
    15.             {
    16.                 return hero.transform.position;
    17.             }
    18.             else
    19.             {
    20.                 Debug.LogError("We should not be accessing his if hero is not initialized");
    21.             }
    22.         }
    23.     }
    24.     public static int getHealth()
    25.     {
    26.         get
    27.         {
    28.             if(hero != null)
    29.             {
    30.                 return hero.health;
    31.             }
    32.             else
    33.             {
    34.                 Debug.LogError("We should not be accessing his if hero is not initialized");
    35.             }
    36.         }
    37.     }
    38.  
    39.     // non-static section of the class. Here you put all the stuff you want to handle within the game object
    40.    
    41.     private int health = 10;
    42.     // It is important to initialize hero every time you go through the Awake function, so that this link
    43.     // is update for every new instance of the Hero Class
    44.     void Awake()
    45.     {
    46.         Hero.hero = this;
    47.     }
    48.    
    49. }
    50.  
    51.  
    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 :)