Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by asduffo · Jul 09, 2013 at 04:10 PM · prefablistgetcomponentloop

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lo0NuhtiK · Jul 09, 2013 at 04:13 PM 0
Share

http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponent.html

avatar image asduffo · Jul 10, 2013 at 01:33 PM 0
Share

yeah i take a look before on it but it explains how to use only for one object, not for multiple objects

avatar image AlucardJay · Jul 10, 2013 at 01:37 PM 0
Share
  • http://docs.unity3d.com/Documentation/ScriptReference/GameObject.html

  • http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponents.html

  • http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponentsInChildren.html

avatar image asduffo · Jul 10, 2013 at 01:58 PM 0
Share

i ended up by doing a little different way but that helped a lot thanks :D

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image asduffo · Jul 10, 2013 at 01:32 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges