- Home /
How do i get script variables from a prefab inside a list?
Hi everyone. Basically, i got a script that renderize some prefabs. They're all stored inside a List (the one from System.Collections.Generic) and all of 'em got another script. I want at some point do something like analyze a variable inside all of the prefabs stored inside the list (all of them have the same script). The problem is that i don't know how to get variables from a script of an object if there are lots of those objects in the scene at runtime. I searched around for a while but all of the documentation looks like to be meant for a specific object use (not for a list of objects stored in a list). I explain better: in code terms, what i'd like to do is something like:
for(int i = 0; i < list.Count; i++)
{
if(list[i].variable == 1) //well that .variable is how i'd do my purpose in other languages like java where you just have to import the name of the script from the bottom, declare an instance and then use it.
{
dosomething();
}
}
sorry for the very noob question.
regard and many thanks in advance.
yeah i take a look before on it but it explains how to use only for one object, not for multiple objects
i ended up by doing a little different way but that helped a lot thanks :D
Answer by Kiloblargh · Jul 09, 2013 at 04:14 PM
The code you have looks fine to me. Did you try it already, and what seems to be your problem?
All you need to do is:
list[i].DoSomething();
if the script in the list has its own DoSomething function,
or:
DoSomething (list[i].gameObject);
if DoSomething is a function on your main script that takes a GameObject as an argument.
no dosomething is a function declared in the main script not in the object script included in all of the components of the list
Your answer
Follow this Question
Related Questions
How do I Instantiate a prefab in a list? 0 Answers
A node in a childnode? 1 Answer
When adding a script using GetComponent<>(), some prefabs that are assigned don't come up 1 Answer
Create a button from an int, remove button when clicked, and never load it again 1 Answer
Interchangeable value loop 1 Answer