Changing Variables on one script with multiple other scripts
I am trying to set up the switch to determine who's turn it is for a 2d-TBS game.
there is a script called BoardManager which constructs the board, and holds the following variables which determines who's turn it is and how many actions they got
PlayerTurn EnemyTurn PlayerActions EnemyActions
The Player script and the Enemy script had to be added to the "headquaters" Gameobject in order to make them function
each of them has a Boardmanager script added to it as well to make GetComponent() work
the problem im having is when one script is finished it's turn, it's supposed to set it's turn to false with boardmanager. X Player Turn = false; and then set the other script to true with boardmanager. X OtherPlayer turn true;
but through debugging i have realized that both the boardmanagers attached to each headquarters are independant from each other, but i can't remove them or else GetComponent() doesn't work,
I need a way to reference only one Boardmanager, and get rid of the BoardManagers on the Headquarters, any ideas?
If it can only be the player's turn or enemy's turn at any given time, you should only have 1 variable in 1 place that indicates that. A boolean has 2 values so 1 boolean can be used to describe whose turn it is perfectly. Having many variables to mark the same piece of information leads into this kind of problems.
Yor problem is here
The Player script and the Enemy script had to be added to the "headquaters" Gameobject in order to make them function. Each of them has a Boardmanager script added to it as well to make GetComponent() work
This tells me you are having trouble figuring out how to reference scripts and variables unless you use them in a specific way. GetComponent can be used on any gameObject in the scene so that shouldn't prevent you from attaching the scripts anywhere you like. When you know how to do that, you can avoid hitting problems like this.
@harko12 's answer is one way to do this; to have a singular script you can easily access from anywhere.
The "Headquarters" objects are prefabs and are instantiated ins$$anonymous$$d of being in the scene
does this affect anything?
Answer by harko12 · Jun 03, 2016 at 03:40 PM
This would be a good time to use a singleton class. That way the player and the enemy could both look at the same BoardManager.
The quick and dirty way to do that is to add a new variable to your BoardManager script, like this:
public static BoardManager instance;
void Awake(){
instance = this; // when the script starts, the instance variable gets set to this instance of the script. Because it is static, there will be only one
}
then in your player and enemy code, to get the BoardManager you just say var manager = BoardManager.instance;
then they will be referring to the same one.
As I said, that is quick and dirty. IF you want to make it fancier, do a search for Singleton on here and I'm sure there will be other examples.
Your answer
Follow this Question
Related Questions
Reading a var from variable parents 1 Answer
how to disable dontdestroyonload on a specific scene 2 Answers
how to keep an animation condition after Application.loadLevel 0 Answers
2D AI movement 0 Answers
Selecting a game object in Unity 2d 3 Answers