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 /
  • Help Room /
avatar image
0
Question by Pamagon · Jan 06, 2017 at 11:11 PM · randomarrayslength

Swapping whole arrays (or their references)

Well this is a tricky one: i'm currently working on a procedural level generator that spawns different prefabs depending on the phase the player has reached. I wonder if there's a way of "swapping" arrays so the whole generation code always references the "currentArray" instead of having multiple copies of the same script with the variable name changed, because that's really inefficient. I wanted to archieve something like this:

 if (beginFirstPhase && !firstPhaseBegun) 
     {
         currPhaseArray = firstPhaseArray;
         firstPhaseBegun;
         beginFirstPhase = false;
     }
 
 if (beginSecondPhase && !secondPhaseBegun) 
     {
         currPhaseArray = secondPhaseArray;
         secondPhaseBegun;
         beginSecondPhase = false;
     }

Unfortunately arrays do not sync unless "currPhaseArray" has ben assigned the same length as the target array, which defeats the whole purpose of using arrays of varying lengths. As far as I know arrays cannot change their length during runtime, and even if such a thing was possible I don't know if it still works after compilation, but if it was it would solve my problem (I think)

Thanks in advance! I've been working on this script for days and it's driving me mad.

Comment
Add comment · Show 2
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 KoenigX3 · Jan 06, 2017 at 11:26 PM 1
Share

You could look through the phaseArrays, and find the longest one. All of the arrays can use that length. If they are the same length, you can easily swap them as you described above.

Let's say that the firstPhaseArray contains 3 elements, the secondPhaseArray contains 4 elements. Both of them could have the length of 4 despite of the first array containing only 3 elements.

When you want to instantiate the gameobjects in the array, you can simply avoid getting NullReferenceException errors with:

 for(int i = 0; i < firstPhaseArray.Length; i++)
 {
     if(firstPhaseArray[i] != null) GameObject.Instantiate(firstPhaseArray[i]);
 }
avatar image Pamagon KoenigX3 · Jan 07, 2017 at 12:43 PM 0
Share

I'll give it a try! Thank you kind stranger

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

89 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

Related Questions

Why UNITY Hates Me By Reading Array's Length! 1 Answer

Spawning random pickups (from Array) into randomly chosen spawn positions (also from an array) and preventing pickups from spawning on top of each other. 0 Answers

Why, when I compare these two random integer arrays with known sum X, do i get the same values for sums of their respective differences? 0 Answers

Change max range attribute? 0 Answers

I'm trying to shuffle an array's order 3 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