- Home /
How to manage a round based game?
As far as I understood scripts in unity are always (?) attached to game objects. But how can I create a script that observes the current running round or level of a game and detect changes in it's state?
Like I want to have for example 3 players, when one finishes his move the next player should be able to move his pawn. Would I attach this script also to some game object?
Also how do I bring all the UI stuff together? Like my 3 players are in the same team and their pawns character portraits should be shown in a list. If there are only two show two, if there are three, show three.
I'm mostly interested in how I an "connect" all these things.
Answer by save · Mar 06, 2012 at 09:09 AM
The easiest approach is to have a manager of some sort. The manager is just a script attached to a GameObject which preferably doesn't destroy on loading new levels. All variables and functions could be static inside the manager so you just have to call something like: GameManager.NextPlayerMove();
//Inside the script called GameManager you can have something like:
static var currentPlayer : int = 0; static var activePlayers : int = 3;
static function NextPlayerMove () { if (currentPlayer<activePlayers) { currentPlayer++; } else { currentPlayer = 0; } }
You'd increase the activePlayers when somebody joins or drops out in the same way via a function.
Regarding the UI stuff you'd use a for-loop for each player.
function OnGUI () {
for (var i : int = 0; i < GameManager.activePlayers; i++) {
//Each player's avatar here
}
}
Check out the GUI Scripting Guide to see what you can do.
Answer by mesho · Mar 06, 2012 at 09:08 AM
Firstly you can attach all the scripts to an Empty game object and then call it "Manager" for example , and for connecting you can use the SendMessage from Unity , it helps you a lot to send orders from a script to another , and sure because your numbers changes in addition to lots of factors you will use a ( Static Variables ) ; it is a pretty rough answer but it is the main guide lines which you need, if you need a big explanation to any of that , just tell here & i`ll give
cheers M:)
It's a good approach except I'd try to leave Send$$anonymous$$essage out if I didn't have to talk between different scripting languages for instance. Send$$anonymous$$essage is a bit slower than calling static functions or variables directly from another script.
Thanks for your answer! But I can't flag both as correct so I've picked the other one that is a little more detailed.
Your answer
Follow this Question
Related Questions
want open source unity3d game + explaination document 0 Answers
Finding a local position matching a global position in an individually rotated hierarchy 2 Answers
How to make an adaptive mobile game? 0 Answers
How does GetRelativeVector work? 1 Answer
Best way to handle high number of usable/pickable objects? 1 Answer