Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 macwoq · Apr 17, 2021 at 07:32 AM · instantiatearraysfloatfor-looparray of gameobjects

instantiate from two seperate values in for loop

Hi, can you please help me i`m lost at this point...

I have two floats in arrays: i.e. float value1[] float value2[]

and I have to instantiate GameObject[] at Vector2 coordinates from these values, I guess this has to be done in for loop but I can`t figure out how to merge these two floats and get a single Vector and loop through all to instantiate...

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

Answer by GameDeveloperAf · Apr 17, 2021 at 09:11 AM

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Example : MonoBehaviour
 {
      public Vector3[] poses;
      public GameObject[] objs;
 
      void Start()
      {
           for(int i = 0; i < objs.Count; i++)
           {
                Instantiate(objs[i], poses[i], Quaternion.identity);
           }
      }
 }


Add objs and poses as same size value and drag drop your objects into objs and set positions to poses and then run the game and all objects will spawn at given positions

Comment
Add comment · Show 8 · 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 macwoq · Apr 17, 2021 at 09:25 AM 1
Share

@GameDeveloperAf

Thanks for the reply, but the point is first I have to somehow merge two floats into one vector and after that instantiate it in a given position, as many times as I have merged Vector2 position...

The main thing here is how to merge floats to vectors...

avatar image GameDeveloperAf macwoq · Apr 17, 2021 at 11:02 AM 0
Share
 public Vector2[] poses;
 public float[] value1, value2;
 
     void Start()
     {
         for(int i = 0; i < 4; i++)
         {
             poses[i] = new Vector3(value1[i], value2[i]);
         }
     }

Do you mean like this? Now each float values will add into vector poses positions. You can merge floats into single vector like this. Any issues?

avatar image macwoq GameDeveloperAf · Apr 17, 2021 at 12:32 PM 1
Share

Any ideas on how to solve "out of range exception" it`s merging and the results are correct but with breaking script error...

avatar image macwoq macwoq · Apr 17, 2021 at 12:11 PM 1
Share

Holy doly works, well there is "index outside of bounds exception" and I have to type manual the size of "poses" array but it really works

avatar image macwoq · Apr 17, 2021 at 02:02 PM 0
Share

@GameDeveloperAf Solved!! Thanks for the help, if you`ll take a look once more how to get rid of this "argument out of range would be great" But even with this is totally working as should.

It`s pointing out of rang in this line: "poses[i] = new Vector3(value1[i], value2[i]);"

Anyway thanks once more.

avatar image GameDeveloperAf macwoq · Apr 17, 2021 at 02:48 PM 0
Share

It is because there is "<=". Just remove equals "=". If you add 2 objects into GameObject[] array then add the same size of array to vector2 and floats of value1 and value2

Just remove "=" inside of "for" loop. I added the equals "<=" by mistake

 for(int i = 0; i < 4; i++)


Is it solved then rename the title to [SOLVED] Good luck

avatar image macwoq · Apr 17, 2021 at 05:09 PM 0
Share

@GameDeveloperAf I wrote Solved because it`s not a problem for me to declare an array size, but yes I still need to fix "out of range" and unfortunately that didn`t solve the problem, it`s not breaking the script, for now, instantiating and all is fine except this little one red console error.

avatar image GameDeveloperAf macwoq · Apr 17, 2021 at 10:39 PM 0
Share

I tested and it is working very well and fixed "out of range" issue. Maybe your arrays sizes not same. Make float value1 and value2, vector2 poses and GameObject objs arrays size same and it would solve "out of range" issue. If you add 2 objects into GameObject[] array then you need add 2 poses into Vector2[] and value1, value2 into float[]. If your array sizes is not same and because of this there you would see "out of range" issue. Make sure all sizes need to be same as in the picture. Can you see that all sizes are same. For example I added 2 sizes of each arrays like in the the picture

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Example : MonoBehaviour
 {
     public Vector2[] poses;
     public GameObject[] objs;
     public float[] value1, value2;
 
     void Start()
     {
         for (int i = 0; i < 2; i++)
         {
             poses[i] = new Vector3(value1[i], value2[i]);
             Instantiate(objs[i], poses[i], Quaternion.identity);
         }
     }
 }

Open image in a new tab and zoom in to take a look

alt text

AND YOU CAN SEE THAT THERE IS NOT ERROR MESSAGE IN THE DEBUG CONSOLE

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

186 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

Related Questions

Switch statement nested in a for loop runs only in one iteration of the loop 1 Answer

Gameobject array 0 Answers

Cannot access 2D array element 1 Answer

For loop comparing two different objects appears to not be running 1 Answer

My Board printing isn't working for my Game of Life automaton, please help! 0 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