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...
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
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...
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?
Any ideas on how to solve "out of range exception" it`s merging and the results are correct but with breaking script error...
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
@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.
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
@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.
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
AND YOU CAN SEE THAT THERE IS NOT ERROR MESSAGE IN THE DEBUG CONSOLE