With my prefabs for player characters I know there are going to be somewhere between 1 and 4. To differentiate, and run methods in the appropriate player object, during that players turn I have to rename them.
In the start of turn I have:
public class Turn : MonoBehaviour {
public int Players;
public GameObject Player;
private GameObject player;
private int[] playerTurn = new int[4];
// Use this for initialization
void Start ()
{
for (int i = 0; i < Players; i++)
{
player = (GameObject)Instantiate(Player);
player.name = "Player " + (i + 1);
}
}
In Player I have:
public class Player : MonoBehaviour
{
private int playerNumber;
public int PlayerNumber {set { playerNumber = value; } }
public GameObject Move;
private GameObject move;
// Use this for initialization
void Start ()
{
//gameObject.name = "Player " + playerNumber;
move = (GameObject)Instantiate(Move);
move.transform.parent = gameObject.transform;
}
What I have works, but I would prefer to name the object after I instantiate it through PlayerNumber
for the sake of keeping things tidy.