I am currently storing a bunch of Vector3 positions in an array. When a user clicks the screen, I am using a Raycast to get the location, sticking it in the array, and then instantiating an object at that location.
I would like to access the properties of those instantiations (ie. their rotations) for other game related calculations. However, my function currently does not create a new variable for each instantiation, so I can’t access the GameObjects.
I was thinking something like this would make sense in my function:
var gameObject[positions.Count] = Instantiate(marker, .., ..);
(actually using a List, hack because of Vector3 array complications). This would hopefully result in something like “gameObject1”, “gameObject2”, etc. How could I append the index number (or count in this case) to the variable name?
Generally, you should make sure a value you initialize (in this case - a GameObject you instantiate), did so successfully.
I’d do something like this:
// Initialize memory
var gameObjects : Array = new Array();
var newGameObject : GameObject = Instantiate(marker, .., ..) as GameObject;
if ( !newGameObject )
Debug.LogError("Could not initialize a newGameObject!");
else // Now you know a GameObject was actually created
{
// Add the GameObject to the array
gameObjects.Add(newGameObject);
// Output the position of the last GameObject added (Coount - 1)
Debug.Log("Position of GameObject: " + (gameObjects[gameObjects.Count-1] as GameObject).transform.position);
}