- Home /
Vector3 Position returns 0 when applied to GameObject
Hello
I have 2 arrays one holds GameObjects (NumberSpace) and the other holds Vectors3 (SpaceArray). I get the position and randomize the Vector3 array all fine but when I tell the GameObject to go to the new Vector3 position from the Vector3 array it returns at 0 or just dose nothing. The goal of this function is to change the position of the GameObjects via the order of the positions in the Vector3 array.
I've been looking for about 2 weeks now I'm giving up and asking for help.
Here is the code.
void PositionChange()
{
SpaceArray = new Vector3[NumbersSpace.Length];//get the length...
for (int i = 0; i < NumbersSpace.Length; i++)
{
//get the positions...
SpaceArray[i] = NumbersSpace[i].transform.localPosition;
//set the position...
//this is were it stops working right...
//NumbersSpace[i].transform.localPosition = SpaceArray[i];
for (int j = 0; j < SpaceArray.Length; j++)
{
NumbersSpace[i].transform.localPosition = SpaceArray[i];
/*NumbersSpace[i].transform.localPosition = SpaceArray[i];
NumbersSpace[0].transform.localPosition = SpaceArray[0];
NumbersSpace[1].transform.localPosition = SpaceArray[1];
NumbersSpace[2].transform.localPosition = SpaceArray[2];
NumbersSpace[3].transform.localPosition = SpaceArray[3];*/
}
}
}
I call the function with a UI button else were in the script. I thank anyone who comes along and helps me with this problem. Thank You.
Answer by Bunny83 · Aug 11, 2020 at 03:15 PM
What is the point of this for loop?
for (int j = 0; j < SpaceArray.Length; j++)
You just execute the exact same line several times. This is completely pointless. Note that the code inside your loop only uses the for loop variable i
from the outer loop. Though I don't see any reason why you would iterate through all objects several times.
You said something about randomizing, but we can't really see anything that looks like randomizing.
If you really just want to randomly swap out positions of the gameobjects the general approach would be
SpaceArray = new Vector3[NumbersSpace.Length];
for (int i = 0; i < NumbersSpace.Length; i++)
{
SpaceArray[i] = NumbersSpace[i].transform.localPosition;
}
// randomize SpaceArray here
//SpaceArray.Shuffle();
for (int i = 0; i < NumbersSpace.Length; i++)
{
NumbersSpace[i].transform.localPosition = SpaceArray[i];
}
To shuffle the array you could use an extension method like this one:
public static class ArrayHelper
{
public static void Shuffle<T>(this T[] arr)
{
for (int i = arr.Length - 1; i > 0; i--)
{
int r = Random.Range(0, i + 1);
T tmp = arr[i];
arr[i] = arr[r];
arr[r] = tmp;
}
}
}
Which this class inside your project you can simply do:
SpaceArray.ShuffleArray();
to shuffle the array.
Thank You so much for your help.
I had to rewrite my shuffle function but it work perfectly now.
Updated Code.
public static void ShuffleObject<T>(T[] array)
{
int tmp = array.Length;
for (int i = 0; i < tmp - 1; i++)
{
int tmpIndex = UnityEngine.Random.Range(0, i + 1);
T t = array[tmpIndex];
array[tmpIndex] = array[i];
array[i] = t;
}
}
$$anonymous$$y PositionChange Function update.
void PositionChange()
{
SpaceArray = new Vector3[NumbersSpace.Length];//get the length...
for (int i = 0; i < NumbersSpace.Length; i++)
{
//get the positions...
SpaceArray[i] = NumbersSpace[i].transform.localPosition;
}
ShuffleObject(SpaceArray);
for (int i = 0; i < NumbersSpace.Length; i++)
{
//set the position...
NumbersSpace[i].transform.localPosition = SpaceArray[i];
}
}
Your answer
Follow this Question
Related Questions
Obtaining an array of positions from an array of gameobjects 2 Answers
C# Array with random.range help? 2 Answers
Find gameobject furthest from player along z axis 0 Answers
What is the best way to instatiate an array of connected GameObjects? 0 Answers
XBox Controller Angle of Fire Incorrect 0 Answers