- Home /
Question by
T27M · May 06, 2012 at 05:30 PM ·
c#instanceaddcomponentforloop
AddComponent Loop
I have a loop that instantiates a prefab multiple times, I want to add a script to each instance with code. Currently they are spawned with a for loop.
var gridx = Instantiate(Grid,transform.position + new Vector3( -220f+j*multi, 0.1f, -220+i*multi ), Quaternion.identity);
gridx.name = "Test Grid X " + i;
How can I add the component ?
I did try gridx.AddComponent but I got an error.
Type `UnityEngine.Object' does not contain a definition for `AddComponent' and no extension method `AddComponent' of type `UnityEngine.Object' could be found (are you missing a using directive or an assembly reference?)
Comment
Best Answer
Answer by Adamcbrz · May 06, 2012 at 05:34 PM
AddComponet is correct but you need to cast you instantiate to a gameobject
Your answer
![](https://koobas.hobune.stream/wayback/20220613071149im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Getting For Loops to work in a Coroutine 0 Answers
iTween RotateTo 1 Answer