- Home /
transform/vector lists & Vector3.Set issues
So i have problems using Vector3.Set, it does not seem to work when using any list.
what does work:
Vector3.Set
vectorlist[i] = vector
transformlist[i].position = vector
what does not work:
vectorlist[i].Set
transformlist[i].position.Set
reading on the web i find it should not work with .position
as this should be a copy not a reference, but this is false, (afaik..) as u can use it as transform.position = vector
(this should not work with copy's).. so why does .Set
not work...? a list should not return a copy (as we can prove this by setting the vector directly using transformlist[i].position = vector
) also if it does not work because of .position
.. why does directly setting a list of vectors not work either ( vectorlist[i].set
)? any suggestions? i really feel like i should report this as a bug but maybe someone has some suggestions or knows what i am doing wrong.
.Set
ONLY works when using a vector3 NOT from a list.
below is some code, supporting this is a scene with an empty game object (StartObject) with 20 cubes as child. pressing S and F changes the positions of the cube while A and D does not
(actually.. D it sets them back to the last value of F (not doing anything with vectorlist[i].set
))
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TESTSET : MonoBehaviour
{
List<Vector3> vectorlist = new List<Vector3>();
List<Transform> transformlist = new List<Transform>();
public GameObject StartObject;
Vector3 tempvect;
// Start is called before the first frame update
void Start()
{
foreach (Transform child in StartObject.transform)
{
transformlist.Add(child);
}
Debug.Log(transformlist.Count);
for (int i = 0; i < 20; i++)
{
vectorlist.Add(new Vector3(0, 0, 0));
}
}
// Update is called once per frame
void Update()
{
//DOES NOT WORK
if (Input.GetKeyDown(KeyCode.A))
{
for (int i = 0; i < 20; i++)
{
transformlist[i].position.Set(Random.Range(0, 10), Random.Range(0, 10), Random.Range(0, 10));
}
}
//DOES WORK
if (Input.GetKeyDown(KeyCode.S))
{
for (int i = 0; i < 20; i++)
{
tempvect.Set(Random.Range(0, 10), Random.Range(0, 10), Random.Range(0, 10));
transformlist[i].position = tempvect;
}
}
//DOES NOT WORK
if (Input.GetKeyDown(KeyCode.D))
{
for (int i = 0; i < 20; i++)
{
vectorlist[i].Set(Random.Range(0, 10), Random.Range(0, 10), Random.Range(0, 10));
transformlist[i].position = vectorlist[i];
}
}
//WORKS AGAIN
if (Input.GetKeyDown(KeyCode.F))
{
for (int i = 0; i < 20; i++)
{
tempvect.Set(Random.Range(0, 10), Random.Range(0, 10), Random.Range(0, 10));
vectorlist[i] = tempvect;
transformlist[i].position = vectorlist[i];
}
}
}
}