- Home /
Create new variable name based on array index
Hello friends,
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?
Thanks
Answer by GuyTidhar · Jul 01, 2011 at 09:34 AM
Have you got an "else" there? Don't insert the reference if it is null - that's why I put an else before doing the "Add" to the array.
Do you know coroutines? You can have all this code inside a coroutine and wait a frame before checking for the object initialization.
For instance:
function CallingFunction()
{
StartCoroutine("AddMarker");
}
function AddMarker()
{
var tempObj : GameObject = Instantiate(marker, selected.point, transform.rotation) as GameObject;
yield;
if(!tempObj)
{
print("failed to instantiate tempobj");
}
else
{
gameObjects.Add(tempObj);
print(gameObjects.Count);
}
}
Hey guyt,
I did miss the else{}, and added it. I also moved my code into the functions like yours, and I'm running CallingFunction() on the mouse click. Everything is working fine in the game screen, however tempObj just isn't getting "instantiated" despite showing up on the screen.
Thanks for your help btw, much appreciated. So I guess the problem lies in tempObj?
Did you use Add$$anonymous$$arker function as I wrote it and called it with StartCoroutine?
try change the line where it says: "yield;" to "yield WaitForSeconds(3);" And see if after 3 seconds from calling this function, whether the print shows correctly.
Are you sure the object "marker" is actually created in the scene? Is it really a GameObject? $$anonymous$$ight it something else? If it is not a GameOject, the tempObj will always be null in this case.
Yeehaw! Yes, marker was originally set as a marker : Transform in the beginning when I loaded it with the inspector. So I guess despite trying to Instantiate(marker) as GameObject, it was still not a GameObject. I simply changed "var marker : GameObject" and everything lined up with your code.
Thanks for your help, I can now access their positions and rotations. :D
Answer by GuyTidhar · Jul 01, 2011 at 08:19 AM
Did you initialize the array of GameObjects? Is it an array?
If it is an constant size array, you should do:
var gameObjects : GameObject[Max_Number_Of_GameObjects];
If it is not constant, you need to use Array or List for the game objects too.
var gameObjects = List.<GameObject>();
And then Add GameObject as you instantiate them
Hey guyt, so I followed your advice about storing my instantiated devices in an array. However, I just realized that Instantiated objects aren't game objects, they're just "objects"? Objects don't have access to the transform class, so I can't get position or rotation. I cannot convert Objects to Game Objects, or at least I'm not sure how.
Otherwise I am now storing these objects in a list. Thanks
Do: gameObjects.Add(Instantiate(marker, .., ..) as GameObject);
And: gameObjects[index] as GameObject;
GameObject is based on Object. When one class is based on another, you can tell the base to be regarded by the program as the second one using the word "as" and setting it into a variable.
Great! So I'm using: gameObjects.Add(Instantiate(marker, .., ..) as GameObject); which seems to be adding the object to the list properly. At least, the list.Count is going up.
However, when I run a test to check the position right after that line: print(gameObjects[i].transform.position);
I receive an error: NullReferenceException: Object reference not set to an instance of an object
Any ideas? Thanks
*EDIT: I just realized my [i] value is meaningless. So I set it to gameObjects[0].transform.position which should retrieve the position of the first object. However, its still returning that same error of nullreferenceexception.
Answer by GuyTidhar · Jul 01, 2011 at 09:16 AM
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);
}
Okay, odd situation.
var tempObj : GameObject = Instantiate(marker, selected.point, transform.rotation) as GameObject;
if(!tempObj){ print("failed to instantiate tempobj"); }
gameObjects.Add(tempObj); print(gameObjects.Count);
So the debug is actually telling me that tempObj is failing to instantiate. However, I see the market appearing right on the screen, so I'm not sure whats going on there. GameObjects.Count is also incrementing fine, but I guess nothing is actually being inserted into the list.
Your answer
Follow this Question
Related Questions
Problem when trying to save variable in another variable. 1 Answer
Array with boolean variables? 3 Answers
Transfering Variables Between Sections [SOLVED] 2 Answers
Custom editor window resets array 1 Answer
Accessing itween variables 0 Answers