Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Kevin__h · May 09, 2017 at 08:54 PM · listrandom

How to assign a variable individually for each element of an object list

 for(int p = 0; p < galaxies.Count; p++) 
         {
             for (int y = 0; y < Random.Range (1, maxSolarSystems + 1); y++) {
                 
                 galaxies[p].solarSystems.Add (ss);
                 
             }
         }


So I'll just start off with the code, I don't know how to correctly ask the question or if it's already posted and solved because I don't know how to phrase the question. Anyways, in the code above galaxies is a list that contains objects (a class called Galaxy) the size of it is determined randomly, thus far it works fine. Now in that class Galaxy there exists a variable called solarSystems, it's a list that contains also objects (class SolarSystem). Now I want to add a random amount of solarsystems to each galaxy instead of having the same amount of solarsystems for all galaxies, but if I do it this way it does take a random amount for each galaxy but adds them and gives each galaxy the added amount... It probably is something easy to fix but I just can't get it. (If you were wondering ss is just an instance of solarsystem)

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

1 Reply

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

Answer by Patrick2607 · May 09, 2017 at 09:15 PM

If you want solarsystem instances added to a galaxy, you need to create a new solarsystem every time, assuming that every solarsystem is a unique one.

 galaxies[p].solarSystems.Add(new SolarSystem());

This will do the job I think. I don't know how you implemented the SolarSystem, but you get the idea. If the SolarSystem is deriving from MonoBehaviour, you'll need to Instantiate it in the Unity-way.

Comment
Add comment · Show 5 · 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 Kevin__h · May 09, 2017 at 09:23 PM 0
Share

Thanks for the reply but that's exactly what I'm doing, ss is an instance of a new solarsystem, so that's not where the problem lies but you might be on to something, I never thought about the instances that get created for my galaxies nor my solarsystems, perhaps the problem lies in the fact that I created an instance Galaxy g = new Galaxy and use galaxies.add(g) so it's that same instance that gets added more than once to list ... thus explaining why the random amount of solarsystems added up ... you my kind sir just fixed my problem and made me realise I'm stupid... thanks for that ... No seriously thanks :D

avatar image Patrick2607 Kevin__h · May 09, 2017 at 09:25 PM 0
Share

I know ss is an instance of a new solarsystem. But you will have to create a new instance every time you want a new solarsystem added to the list. Now you are just adding the same instance over and over again.

avatar image Kevin__h Patrick2607 · May 09, 2017 at 09:30 PM 0
Share

I know, I was doing exactly the same with my galaxies and that was what caused the problem. Tho not creating new instances for solarsystem didn't cause any problems at the moment it probably will later on, so I changed it as well. I get the logic behind it, I understand why it causes troubles I'm just baffled by the fact that I never even considered the instances that got created (or in my case, didn't get created). So thanks for your quick replies and help.

Show more comments
avatar image Kevin__h · May 09, 2017 at 09:37 PM 0
Share

Thanks, it has to be done within half an hour tho :P So your quick replies might get me to finish it before the deadline :D

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

70 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

Related Questions

A node in a childnode? 1 Answer

How to Detect Touch on a Random String from a List? 1 Answer

how to create random list of two diffrent prefab 1 Answer

Creating a record of values generated with Random.Range 1 Answer

Spawn various enemies at random 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