- Home /
Question by
zerophase · Nov 26, 2013 at 05:47 AM ·
javascriptinstantiateprefabname
Assigning a prefab a number suffix at runtime.
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.
Comment
Best Answer
Answer by Xelareip · Nov 26, 2013 at 01:48 PM
I'm not sure what you're asking here but if I understand it right you want your GameObject's name to depend entirely on the playerNumber?
If that's right you can probably change the PlayerNumber property as following :
public int PlayerNumber
{
set
{
playerNumber = value;
gameobject.name = "Player " + playerNumber;
}
}