- Home /
problem of switching character in the game
Hi,
I want to make a game which is for two players. when I press the button, the character which was chosen by 1P will move, after that if I press the button again, the character which was chosen by 2P will move, too. Just like the monopoly.
I finished the menu which is for character selection, and it can work nomarlly. The system can determine what character is for 1P and 2P.
There is a script called "Move". It is attached to every character and it can let character move after click the button. Script "Move" is successful,too. Unfortunately, it only can work normally when just one player.
So, I have a problem, The process "switch character" is failed, can someone help me to solve this?
This is my script called "DiceNumber". this script is used to create a random number and is attached to an empty game object:
static var num:int; // dice number (random number)
var script:Move; // the script attached to every character
static var shouldMove:boolean = false;
var savedName : String;
var savedName1 : String; // character’s name which is chosen by 1P
var savedName2 : String; // character’s name which is chosen by 2P
var player:int = 2; //how many plays
var turn:int = 1; // whose turn
function Awake(){
savedName1 = PlayerPrefs.GetString("PlayerName1"); //Get character’s name chosen by 1P from character selection menu
savedName2 = PlayerPrefs.GetString("PlayerName2"); //Get character’s name chosen by 1P from character selection menu
}
function ChangeNumber(){
num=UnityEngine.Random.Range(1, 7);
shouldMove = true;
script.Go(); //call Move.Go(), let character move
}
function OnGUI(){
if(GUI.Button(new Rect(20, 240, 50, 50), "click")){
turn++;
ChangeNumber();
}
}
function Update () {
if(turn>player){
turn = 1;
}
for(var i=1; i<player; i++){
if(turn==i){
script = GameObject.Find(savedName+i).GetComponent(Move);
}
}
}
Your answer
Follow this Question
Related Questions
How do I change Sprites (and manipulate the Sprite Object) in C#? 0 Answers
GetComponent of ALL clones? 2 Answers
Getting a Script without setting a variable to the script name 1 Answer
What is the order of execution for the instantiated object's GetComponent.Script.DoSomething? 1 Answer
Camera switch between child back to main camera issue 2 Answers