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 Zammystuff · Mar 14, 2021 at 09:22 PM · listnoobforloop

Trying to diagnose this For Loop issue, for instantiating from a list.

New to Unity/Programming, so sorry if this is a dumb question.

This function controls the spawning of weapon parts in a weapon customization menu. Its also the function i reference in my .AddListener, and delegate.

The idea is that using the dropdown.value function, I can set an integer to the index of the drop down, then using a for loop, I can iterate through the List that the barrels are kept in, and ensure that when the Index of the Dropdown changes, the correct index of the list is referenced, and then spawns the correct barrel.

Basically my thought process is:

  • Find the Index that the Dropdown is on.

    • Store that index, and run through the list the gameobjects are stored in.

  • When the Index of the List is Equal to the Index of the Dropdown, stop the Loop, and instantiate the gameobject that is in that index of the List. And when the Index of the List != the index of the drop down, destroy the current gameobject, and spawn the correct one.

Apologies if this isnt formatted correctly

 void spawnBarrels(int value, List<GameObject> temp, Transform vec)
 
 {
 
 var index = barrelDropDown.value;
 
 for (int i = index; i == barrels.Count; i++)
 
 {
 
 barrelListIndex = i; //doesnt seem like this is the correct way to do this
 
 break;
 
 }
 
 if (barrelListIndex == index && maxSpawn == 0)
 
 {
 
 GameObject tempObj = Instantiate(barrelList[barrelListIndex]);
 
 tempObj.transform.position = vec.position;
 
 currentActiveBarrel = tempObj;
 
 Debug.Log(
 currentActiveBarrel.name);
 
 maxSpawn += 1;
 
 if (index != barrelListIndex)
 
 {
 
 maxSpawn = 0;
 
 Destroy(currentActiveBarrel); //doesnt destroy when the barrel changes
 
 }
 
 }
 
 //sorry my code is shit
 
 }

Problems:

The barrels dont change when the drop down changes, but I can see the index's changing in the inspector.

The barrels do not destroy when the value's change.

Im kinda stuck on this, and im not sure exactly how to diagnose this, theres no errors, and Im banging my head against the wall. My theory is that im not quite understanding how the for loop works, but from everything I read/understand this should be working.

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

0 Replies

· Add your reply
  • Sort: 

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

118 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

Related Questions

A node in a childnode? 1 Answer

How to reference a multidimensional list? 1 Answer

Why does this script freeze Unity 2 Answers

Randomize Text on 4.6 UI Button's 1 Answer

JS: Access sections of an array, without activating the rest 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