Sending a variable to many gameObjects

Is there a more elegant way of sending a var to many gameObjects that share the same script without repeating myself? Right now am doing something like this…

It’s a sports game so i need a way for the players to exchange information about possession, state etc, what would be a good approach to handle communication without a “brute force” method like the above?

Thanks.

Change it to have a method to set the variable instead, and use delegates. Or possibly getters/setters - I haven’t tried adding those to delegates, but perhaps it works. To the MSDNmobile!

If you have multiple variables named something like: “player#Script” where # increments for each one…

Maybe you should be using a List or Array.

Then you can access them by index, or loop over them.

I’ll look into delegates, thanks.

So they are like a variable that holds a function instead of a value?

Delegates can hold multiple functions, which is a neat bonus feature. But yeah, rearranging your code to have a central spot with a list of data might be better. The warning sign for when that would be the case is when you use numbers in variable names :slight_smile:

Indeed i could, although it doesn’t solve the design issue at hand, ideally only the affected objects should be exchanging any information.

And why would that be?

If you have player1, player2 etc. variables you are repeating functions. Better to reference player[n] as needed.

1 Like

You could parse the tag for a number… but

Maybe this?

PlayerScript ps = other.GetComponent<PlayerScript>();
if(ps == null) return;
int index = ps.playerNumber;
for(int i = 0; i < allPlayerCount; ++i){
   if(i == index) {
      playerList[i] // isActive is enable or a variable? .. either way, do that here.
      continue;
    }
    playerList[i] // again, whatever.. here.
  }

Where ‘playerList’ is of course a list or array. =)

My concern is that these collisions can fire hundreds of times per second when the ball is being contested and squeezed between players and i can’t be updating all other unaffected scripts when that happens, i might be adding more actions in the future and it would get wild pretty fast.

I can’t be sure, but perhaps you want a simpler version like “ActivePlayer” (guessing from how your code was), and then you can simply do "if(ActivePlayer) " or “if(!..” for whatever those scripts do. I dunno. :slight_smile:

Edit/add: I can’t think too deeply atm, but the above idea may require some list to track, as more than 1 may be in contact… will be interesting to see the update here, next time I’m back.

You might need to refactor things a bit so that you don’t do any unnecessary testing. When detecting collisions there are clear ways to only get the objects involved, so you shouldn’t be referencing more than you need. Maybe describe it in more detail.

To try and paint a rough picture of the whole thing… there’s a ball object with functions such as Kicked, Trapped, StuckToOwner, Dribbled etc which are called from a player and are fed its transform so that forces are applied accordingly, (i am using non-kinematic rb’s for everything), that’s all that goes into being our ball.

When a player is in possession of the ball after colliding with it he also gets tagged as “owner” using a bool in order to drive the AI and steering behaviours of him and all other players, for example a player will only tackle if there’s a player controlling the ball.

So at every collision between the ball and any player that isn’t the owner i need to call a status update between the scripts of new and previous owner in a better way than what i have above.

I’ve been trying to avoid the “game manager script” concept but it could also serve as a sort of commentary system so perhaps it’s the better way in this case.