Reputation: 111
I'm not doing a great game on unity. I want to make a save function. It will work so that when you click on the button, each object in unity will be recorded, along with its Position scripts and so on. But I did not understand how to sort through all the objects on the stage, I hope that someone from those sitting here will help me. There may be errors in the text, I used google translator.
Upvotes: 0
Views: 1509
Reputation: 945
You can get all the objects in a scene and iterate over them to find the desired data by:
//GameObject[] allGameObjects = FindObjectsOfType<GameObject>(); // Ignore this as per derHugo's comment, use the line below
GameObject[] allGameObjects = SceneManager.GetActiveScene().GetRootGameObjects();
foreach (GameObject go in allGameObjects)
{
Debug.Log("Name: " + go.name);
Component[] components = go.GetComponents<Component>();
foreach (Component comp in components)
{
Debug.Log("Component: " + comp.GetType());
}
}
Edit: As derHugo pointed out in the comments on this answer, using FindObjectsOfType will only return active GameObjects, so I've modified the answer. You will also need to add using UnityEngine.SceneManagement;
If you want to save objects in other scenes you can target them by name or index using other methods and properties of the SceneManager class
Upvotes: 1