- Home /
Question by
zmeu5 · Jul 15, 2016 at 12:12 PM ·
spawnprefabsprefab-instancecomponents
How to add components to all spawned prefabs ?
This is script is attached to a prefab that is spawned two times. But awake or start works only for one object. How can I run this code for both, when object is spawned.
void Awake () {
gameMenu = GameObject.Find("GameMenu").GetComponent<GameMenu>();
mainMenu = GameObject.Find ("MainMenu").GetComponent<MainMenu> ();
score1 = GameObject.Find ("Score1");
score2 = GameObject.Find ("Score2");
deckController = GameObject.Find("GameMenu").GetComponent<DeckController2>();
Dice = GetComponent<DiceController>();
Dice.Dices [0] = GameObject.Find("Dice1");
Dice.Dices [1] = GameObject.Find("Dice2");
Dice.Dices [2] = GameObject.Find("Dice3");
Dice.Dices [3] = GameObject.Find("Dice4");
Dice.Dices [4] = GameObject.Find("Dice5");
Deck = deckController;
currentVariant = (int)Buttons.none;
currentState = (int)States.waiting;
mainMenu.gameObject.SetActive(true);
gameMenu.gameObject.SetActive(false);
}
Comment
there must be some mistake, Awake is called for each component/script when they are instantiated/created. I confirmed it by trying a simple debug in awake.
Your answer
![](https://koobas.hobune.stream/wayback/20220612084856im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Problem: Random Instantiating more than one prefab? 1 Answer
prefab navagent doesnt work,prefab navmesh doesnt work 1 Answer
Changes done in prefab script get applied to all instances of that prefab. 0 Answers
Different way to spawn prefabs 0 Answers
Instantiating Mesh Error - Mesh has Instance added? 1 Answer