Hi Unity,
I’m trying to find a way to convert a single-player based inventory system to a multiplayer one. I’m having trouble finding a way to iterate through the server’s GameObjects using tags to find that GameObject that contains “my” networkView. Doesn’t findGameObjectsWithTag act as a for-loop essentially? Can I therefore do something like this?
void LookForObject () {
foreach (GameObject objectIWantToFind in global) {
if(objectIWantToFind.transform.tag == "TagIWantToFind" && objectIWantToFind.transform.GetComponent<NetworkView>().isMine){
//do the thing I want on this object
}
}
}
}
Or Perhaps this
void LookForObject () {
GameObject ThingIWantToFind = GameObject.FindGameObjectsWithTag ("TagIWantToFind");
foreach (object.GetComponent<NetworkView>() in ThingIWantToFind) {
if(networkView.isMine){
//Do The Thing I Want
}
}
}
Generally it’s better to maintain your own list of similar Gameobjects (i.e. players) than use tag finding because it’s more performance-friendly. You have the right idea, except you need to iterate Gameobjects in an array of Gameobjects and you need an array of Gameobjects to equal GameObject.FindGameObjectsWithTag… because it returns an array. Tl;dr: Use the following code.
GameObject[] Players = GameObject.FindGameObjectsWithTag ("Players");
foreach (GameObject object in Players)
{
if (object.getComponent<NetworkView>().isMine)
//DoLeThingIWant()
}