I need a list of positions from several gameObjects tagged as “Wall”. I have searched all over, but only found specific answers to others’ problems.
Would I use ‘for()’ or ‘foreach()’ here?
In my LevelScript is:
public Vector3[] GroundWallPosition;
And in my otherScript
public GameObject[] GroundWallObjects;
void Start ()
{
GroundWallObjects = GameObject.FindGameObjectsWithTag ("Wall");
foreach (GameObject Wall in GroundWallObjects)
{
GetComponent<LevelScript>().GroundWallPosition[Wall] = GroundWallObjects[Wall].transform.position;
}
}
But obviously I cannot directly convert a gameObject into a Vector3, so how would I go about doing this?
This is pretty simple, one way you can do this…
public LevelScript script; //drag script into here
public GameObject[] GroundWallObjects;
void Start()
{
GroundWallObjects = GameObject.FindGameObjectsWithTag ("Wall");
for(int = i; i < GroundWallObjects.Length; i++)
{
script.GroundWallPostion _= GroundWallObjects*.transform.position;*_
}
}
jokim
2
You’re using a foreach loop as if it was an index based loop…
I don’t believe you can use “Wall” as an index.
Try converting to a Index loop
public GameObject[] GroundWallObjects;
void Start ()
{
GroundWallObjects = GameObject.FindGameObjectsWithTag ("Wall");
for (int i = 0; i < GroundWallObjects.Length; i++)
{
GetComponent<LevelScript>().GroundWallPosition _= GroundWallObjects*.transform.position;*_
* }*
* }*