- Home /
Question by
haidao0923 · Jul 27, 2018 at 07:20 PM ·
script.listgetcomponent
Get Script from every gameobject in a list
So I have a list with 5 of the same game object created from a prefab.
during runtime, I changed the value of the created gameobject, but it doesn't affect the script obtained from the prefab gameobject. How can I do
"private [Insert Scriptname Here] "variable name here" to collect the script from the created gameobject rather than the prefab?
private Laser laserScript;
private Rigidbody2D rb;
public List<GameObject> pooledLasers;
public GameObject laserToPool;
public int laserAmountToPool;
private void Start()
{
laserScript = laserToPool.GetComponent<Laser>();
rb = GetComponent<Rigidbody2D>();
for (int i = 0; i < laserAmountToPool; i++)
{
GameObject laser = Instantiate(laserToPool);
laser.SetActive(false);
pooledLasers.Add(laser);
}
}
so the script only Get the Script of the prefab, how can I make it get the script of the created gameobject that is in the list
Comment
Best Answer
Answer by hexagonius · Jul 27, 2018 at 07:46 PM
Do the GetComponent on laser, the variable you assigned the instantiated gameobject to