- Home /
Using Vector3[] to store vector3 of objects,
Im using emptygameobjects as positions to instantiate objects as their spawn positions. Is there a reason I cannot drag and drop an emptygameobject in the Inspector to the Vector3[] Array? since I assume im using this array wrong. How do I pass a Vector3 of a gameobject to my vector3 array list? thanks in advance, code I wrote is below.
{ public Vector3[] spawnPoints; public GameObject[] obstacles; public float spawnRadius;
private void Start()
{
//int spawnPointIndex = Random.Range(0, spawnPoints.Length);
//int obstaclesIndex = Random.Range(0, obstacles.Length);
int howmanytospawn = Random.Range(0, 10);
for (int i = 0; i < howmanytospawn; i++)
{
int spawnPointIndex = Random.Range(0, spawnPoints.Length);
int obstaclesIndex = Random.Range(0, obstacles.Length);
if (!Physics.CheckSphere(spawnPoints[spawnPointIndex],spawnRadius))
Instantiate(obstacles[obstaclesIndex], spawnPoints[spawnPointIndex], Quaternion.identity);
}
}
Answer by NoodleCup · Jul 02, 2019 at 02:21 AM
To answer this question to anyone that comes googling after me, I was trying to use vector3 to store positions of items. after much googling and understanding my ideocracy. You should use transforms to store positions of objects. Instead of using vector3 spawnPositions I used a transform spawnPositions and in my instantiate area of the code I had to add transform.position to the location part of the function.
so
public Transform[] spawnPoints;
Instantiate(obstacles[obstaclesIndex], spawnPoints[spawnPointIndex].transform.position, Quaternion.identity);
fixed my understanding of vector3s and transforms then I was easily able to differentiate the use of each. cant use vector3 when you're looking for a world pos. goodluck.
Just to clarify (for any future readers) - a Vector3 can be used to store the position of an item, but it only stores the position at that moment in time. Useful for backing up a previous position etc.
If you want to be able to get an up-to-date item position, then yes - Transform is certainly the class you need, and good work figuring it out.
Your answer
![](https://koobas.hobune.stream/wayback/20220612204206im_/https://answers.unity.com/themes/thub/images/avi.jpg)