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 /
  • Help Room /
avatar image
0
Question by firestorm185 · Jun 20, 2020 at 01:57 AM · prefabslistsinteger

Select a prefab from a list variable using an index by an integer variable?

I'm using a button in a VR game to change the hand model of the player (a skins system)

and I created a public list of GameObjects that I've dragged the prefabs I need to choose from into. I've then created a public integer variable, labelled "Skin" that I can access from other gameobjects,

My issue is I need to use that integer "Skin" to select a prefab from that position in the list of GameObject and apply it to the model slot in another script. I figured out how to set the variable in another script, I just don't know how to pull that prefab out from the list of prefabs once it's put in.

I figured I could use a seperate variable, a private GameObject variable, and set that private variable to that index'd prefab from the list to then call later, I just don't know how. Any help would be a lifesaver!

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by firestorm185 · Jun 20, 2020 at 02:55 AM

figured it out! it was waaaaaay simpler than I thought. For anyone who finds this in the future, this is how you grab an item with an integer variable from a public list -

    public list<"variable type here"> List1;
     public "Variable type here" Object1;
     public integer Skin = 0;
     
     public void SkinChange()
     {
     
     Object1 = List1[Skin];
     // This is the important part. You take the integer "Skin" in the list "List1" as the spot in the list you want
     //to take the item from, and set it as equal to Object1, your premade variable. That way, once you place
     //all your items in the list from Unity's GUI, you can access that object later and do whatever you want
     //with it. Cheers!
     
     }














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 firestorm185 · Jun 20, 2020 at 03:05 AM 0
Share

Well I mean, it's "my answer" but ok Unity, go ahead, say it's the best. I won't judge. xD

avatar image
1

Answer by N-8-D-e-v · Jun 20, 2020 at 02:39 AM

This is actually pretty simple, first create an integer variable, this will select the gameobject from the list

 List<GameObject> yourList = New List<GameObject>();
 int finder;


Then set that variable to a range between 0 and the length of your list (the first item in every list and array has an index of 0)

 void Start()
 {
      finder = Random.Range(0, yourList.Length); //random number between first index and last index
 }

NOTE random.range must be used in a method. After you've done that, just set that private gameobject variable you were talking about to the finder variable index in your list, like so

 [Your Private GameObject Variable] = yourList[finder]; //this will get the item in the list with the index  of whatever the random number was


And there you go! Let me know if you need anything else, cheers.

Comment
Add comment · Show 4 · 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 firestorm185 · Jun 20, 2020 at 02:58 AM 0
Share

Thanks Curtz! I actually found an even simpler method to calling a specific item from a list. I just treat it like an Array and ins$$anonymous$$d of asking for a numbered index of the array, I put the integer variable I created in ins$$anonymous$$d! Like -

 public list<Transform> List1;
 public Transform Object1;
 public integer Int1;
 
 void Update()
 {
 Object1 = List1[Int1];
 }

avatar image firestorm185 · Jun 20, 2020 at 03:02 AM 0
Share

$$anonymous$$y issue was (and I've read a lot of different answers around that answered it similarly to your answer) was that my index position was chosen by a public variable that's set by an interaction with an XR component, not a random number, so I needed to know how to call for a specific variable at a specific number ins$$anonymous$$d of a random, which is all the answers I had been seeing. XD

Thanks again for the help though! I appreciate it!

avatar image N-8-D-e-v firestorm185 · Jun 20, 2020 at 02:46 PM 0
Share

I see, I haven't done anything with vr yet, but I'm happy to help!

avatar image N-8-D-e-v firestorm185 · Jun 20, 2020 at 02:47 PM 0
Share

Actually, while I'm here, could you point me towards any resources for game dev with vr?

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

210 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Updating and redrawing list objects - How? 0 Answers

Only spawning power ups that the player wants in that game 1 Answer

How to add prefab children to a list in unity 0 Answers

Adding prefab objects to a list on another script and object? 2 Answers

For some reason I cannot drag my prefabs from the Project folder into the scene hierarchy. 2 Answers


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