- Home /
Store Vector3 from Array of GameObjects to Vector3Array
Hey there! I'm looking for some help concerning my problem here. Well, from one script I instantiate object that I track in this script as a array of GameObjects with FindGameObjectsWithTag. It is working as a charm, but I just cant get the Vector3 of those GameObjects and store them in their own array.
So here is the script in question:
public GameObject holeBlockPrefab;
public GameObject[] holeBlocks;
public Vector3[] holeBlocksPositions;
public int holeBlocksCount;
public int loadHoleBlockCount;
void Awake() {
if(PlayerPrefs.GetString("LoadType") != "NewGame") {
loadHoleBlockCount = PlayerPrefs.GetInt("HoleBlocksCount");
if(loadHoleBlockCount != 0) {
SpawnObjectOnLoadSaveGame(holeBlockPrefab);
}
}
}
void Update() {
holeBlocks = GameObject.FindGameObjectsWithTag("Hole");
holeBlocksCount = holeBlocks.Length;
if(Input.GetKeyDown(KeyCode.F5)) {
PlayerPrefs.SetInt ("HoleBlocksCount", holeBlocksCount);
Debug.Log("Saved hole blocks count.");
}
}
By Vector3 you mean the position of the objects?
Answer by Paulo-Henrique025 · Jul 25, 2014 at 04:34 PM
Try something like this:
public List<GameObject> holes = new List<GameObject>();
public List<Vector3> positions = new List<Vector3>();
void SavePositions()
{
foreach(GameObject g in holes)
{
positions.Add(g.transform.position);
}
}
Thank you, sir! :) I was so angry that I can not make it work that I totally forgot that I can use a List. :)
Your answer
Follow this Question
Related Questions
List and store vector3 of game objects with tag 1 Answer
Setting position of a transform? 2 Answers
if close to one of a kind? 2 Answers
NGUI - Vector 3 Position different from position 1 Answer
How can I simplify this script? 0 Answers