Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Is Unity finding objects in the same order with FindGameObjectsWithTag for all clients?

Discussion in 'Scripting' started by eliasjgnilsson, Dec 20, 2021.

  1. eliasjgnilsson

    eliasjgnilsson

    Joined:
    Mar 1, 2021
    Posts:
    77
    Hi,

    I have a game where players can hit explosives. I want everyone to see when another player hits a certain explosive and I think sending information as an RPC will be a good idea for this (using Photon PUN).

    The explosives are all tagged "explosive" and are activated at start of the level.

    The question I have is how to know what explosive someone else hit. I was thinking of caching each explosive object locally like this:

    Code (CSharp):
    1. foreach (GameObject explosive in GameObject.FindGameObjectsWithTag("explosive"))
    2.     {
    3.       explosivesList.Add(explosive);
    4.     }
    Then when a player hits an explosive I find the index in the explosivesList and send it over RPC. Then each player can activate the explosive locally.

    The main quesiton with this approach is: Does Unity find all gameobjects by tag in the same order all the time, or is there a risk that the list is differently ordered for different players? Or can objects even get in different positions in the heirarchy after build and get mixed up that way too?
     
  2. PizzaPie

    PizzaPie

    Joined:
    Oct 11, 2015
    Posts:
    103
    Just add some kind of identification on your explosive game objects, either via script or by giving them unique names.

    Overall you shouldn't rely on oder of objects returned from a Find function (technically you shouldn't rely on find methods altogether) or a Get Components function, even more so on multiplayer game.

    Edit: Given that explosives are already present at the start of the map nothing prevents you from caching those references to any other script present.
     
    Last edited: Dec 20, 2021
  3. eliasjgnilsson

    eliasjgnilsson

    Joined:
    Mar 1, 2021
    Posts:
    77
    Yeh I know, I should probably have some other unique identifyer. Problem is I don't want to rely on string names either since I might change those. Any ideas on how to do this the best way?

    I am caching them with FindGameObjectsWithTag at start up of each level. I just need to know if the order of caching is the same when doing it on my computer and on some client.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    Every explosive has a position, right?
     
  5. eliasjgnilsson

    eliasjgnilsson

    Joined:
    Mar 1, 2021
    Posts:
    77
    Yes they each have a transform, but they sometimes move with script
     
    Last edited: Dec 20, 2021
  6. PizzaPie

    PizzaPie

    Joined:
    Oct 11, 2015
    Posts:
    103
    Well either create a component with that functionality or add the logic on existing component. As for the actual implementation in general you can use System.Guid class to generate new guid (will need to cast to string otherwise won't get serialized) and use either serialization or OnValidate callbacks to assign the value/ save/ check against other similar objects.

    I believe I ve seen several implementations in github under MIT license so you can google something like "Unity Generate persistant GUID"