Question by
acroscene · Dec 30, 2020 at 09:34 PM ·
gameobjectarrayarray of gameobjects
create array of specific gameobjects
I got two of the same gameobjects in Unity and I want to specify which one to destroy. My idea was to put them into an array to be able to select which one the two "MyGameobject" to destroy. I tried this:
private void Awake()
{
int gameStatusCount = FindObjectsOfType<GameSession>().Length;
// this returns 2 gameObjects.
// I was thinking of doing something like this but not sure it is the right way of doing it
GameSession[] objects = new GameSession[2];
int i = 0;
foreach(GameSession gameSession in FindObjectsOfType<GameSession>() )
{
objects[i] = gameSession;
i++;
}
Debug.Log("length 1 : " + objects.Length);
Destroy(objects[1]);
Debug.Log("length : " + objects.Length);
I also tried this but obviously the wrong syntax.
private void Awake()
{
int gameStatusCount = FindObjectsOfType<GameSession>().Length;
// this returns 2 gameObjects.
// I was thinking of doing something like this but got the wrong syntax
GameObject[] objects = FindObjectsOfType<GameSession>();
Destroy(objects[0]);
How can I do this?
Comment
Your answer
Follow this Question
Related Questions
How do I access an object script variable that is in a 2D array? 0 Answers
Need to randomly place Waypionts from an Array in an "closed" 2D space 0 Answers
Multiple Instantiations not working 0 Answers
Getting Collision from an array and changing properties from other gameobjects in that array 0 Answers
NullReferenceException: Object reference not set to an instance of an object 0 Answers