- Home /
beginner question: behavior of duplicated objects
Im new to unity, and trying to figure out the best way to work out a game mechanic. I have a game object that is duplicated around 100 times. The longer the user performs an action i.e. push the f key, the more the objects will disappear until they are all gone. As the objects are named like object-1, object-2, etc. it seems to me that there should be someway that I just increment a counter and have the same script run to remove each object. Is this correct and if so what do I attach the script to or does it need to be attached to each individual object?
@Grorg I would suggest that you create a Prefab, and call Instantiate(Prefab, parent)
to create your objects. At the same time, you add them to a list. When you need to destroy them, simply call destroy, and remove it from the list.
public GameObject prefab; //assign this in the inspector
List<GameObject> listOfGameObjects;
void OnStart()
{
listOfGameObjects = new List<GameObjects>();
for (int i =0; i < 100; i++)
{
GameObject temp = Instantiate(prefab, transform); //assu$$anonymous$$g you've attached this
//script to the parent
listOfGameObjects .Add(temp);
}
}
public void DestroyObject() // Assign to 'f' press event
{
GameObject temp = listOfGameObjects [listOfGameObjects.Count-1];
Destroy(listOfGameObjects [listOfGameObjects.Count-1]); // removes the last element
// added
listOfGameObjects.Remove(temp );
}
You can make one script on separeted GameObject lets say "Controller". Next when you click F then you can randomly destroy the object [objects]. $$anonymous$$eep all objects in an array.
Thanks! Had to do a couple of small changes, but it was a big help. I will post my code once I have it a little further refined.
@Grorg You're welcome! Do note that Instantiate()
and Destroy()
are resource intensive, so if you intend to hide and display the object repeatedly, you'd be better off using gameObject.SetActive(true/false)
or moving the object out of sight (eg. below the game plane).
I made the mistake of trying to repeatedly destroy and re-instantiate several hundred objects every second, which totally hung the game. Had to do quite a bit of re-coding to correct the problem.
Answer by Igor_Vasiak · Apr 28, 2017 at 12:09 PM
Create a script and type this at OnMouseStay loop (it doesn't matter if it's C# or JavaScript, it will work for both):
if (Input.GetKeyDown(KeyCode.F)) {
Destroy(gameObject);
}
Then select all of your objects by typing on the search label at the Hierarchy window: object, then select the first one, scroll down, hold SHIFT and select the last one and drag your script at the Inspector window.
Note: Just know that, if you drag one of those objects to the project tab you'll have a prefab with all of the attributes you've placed at the object. Then all you'll need to do is drag ONE object prefab to the Hierarchy and it doesn't matter how much copies of the prefab you have, each modification you make on it will be applied to each one of them. Hope I answered your question.
Your answer
Follow this Question
Related Questions
need particle help 1 Answer
Can someone explain 'Using ..." and "MonoBehaviour" in C# 7 Answers
Unity Scripting armv7 Error 1 Answer
Good or Bad ending 1 Answer
Script that switches between first and third person controller 3 Answers