[HELP C#] Making Gameobject 1 equal Prefab 2
I need help on a script I am working on (C#). I have an animator that animates a GameObject called player (it animates its position, scale and active state). In a script I have, I have an array of GameObjects called players where I put references for player_ prefabs, and a public GameObject called player, where I put a reference for my player. I am trying to make the following script:
player = players[i]
This does not work, and the player remains the same. I do this at Start(). I really want to know what I should do.
For further info: the player has a transform and spriterenderer components, while the player_ prefab has transform, spriterenderer (with another sprite) and animator.
Thanks for the attention!
Answer by phosphoer · Jan 28, 2016 at 06:44 AM
I think what you are asking is: "How do I choose which player prefab to use out of an array of player prefabs?"
If so, you probably want something like this:
private void Start()
{
var i = Random.Range(0, players.Length);
player = Instantiate(players[i]);
}
But in my animation I am using the player, and the animation moves it etc. I waant the player_ prefab to substitute the player, and keep the name player, just change the components and its values.