How to add names of FindGameObjectsWithTag to list?

Hi,

I’d like to know how can I add all names of founded object to a list?
Whit the debug I see 5 name, but only the first found has been added to my Game list.
Any idea how can I add the others?

foreach(GameObject AllgameObjectsSave in GameObject.FindGameObjectsWithTag("Player"))
                    {
                    Game.current.savedPrefabName.name = AllgameObjectsSave.name;
                    Debug.Log (AllgameObjectsSave.name);
                    }
using UnityEngine;
using System.Collections;

[System.Serializable]
public class Game {

    public static Game current;
    public Character vezetek;
    public Character osszeallitas;
    public Character savedPrefabName;



    public Game () {
        vezetek = new Character();
        osszeallitas = new Character();
        savedPrefabName = new Character();
    }
    
}

I dont see anywhere in the code that would add a string to a list. is Game suppost to be the list? It appears to just be a current variable.

You have right.
I have added at the top:
publicList prefabLista = newList();

foreach(GameObject AllgameObjectsSave in GameObject.FindGameObjectsWithTag("Player"))
{
prefabLista.Add (new Game(AllgameObjectsSave.name));
}
using UnityEngine;
using System.Collections;

[System.Serializable]
public class Game {

    public static Game current;
    public Character vezetek;
    public Character osszeallitas;
    public string savedPrefabName;



    public Game ()
    {
        vezetek = new Character();
        osszeallitas = new Character();
    }

    public Game (string sPName)
    {
        savedPrefabName = sPName;
    }

}

How can I save them with “Game.current.”?

I dont know how you would use current in order to save the name. Although, how come it needs to be current instead of string? Add all of the names of the gameObjects to a String List instead, that would make more sense.

1 Like

I give you much more information. I’d like to save my gamestate in a local file.
First I would like to save names of all object to file, then the positions of the objects.

If I use // Game.current.savedPrefabName = AllgameObjectsSave.name;
then only the first found object has been saved.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class felhasznalo : MonoBehaviour {
 
    public GUIStyle userStyel;
    public GUIStyle userTooltipSytle;
    public GUIStyle szoveg;
    int counterAT=0;
 
//    string email = "";
//    string username = "";
//    string password = "";
//    string confirm = "";
 
    public enum Menu {
        MainMenu,
        NewGame,
        Continue
    }
 
    public Menu currentMenu;
    public List<Game> prefabLista = new List<Game>();



//    public void AddTarget(Transform savedPrefabTransform){
//        prefabLista.Add (new Game()); 
//     
//    }



    void OnGUI () {
     

        if (counterAT==2)
        {
            counterAT = counterAT-2;
        }
     
        if (GUI.Button (new Rect (Screen.width/2+35, 10, 50, 48), "" ,userStyel))
        {
            counterAT++;
            Debug.Log(counterAT); 
        }
     
     
        if (counterAT == 1)
        {
        GUI.Box (new Rect (Screen.width / 2 - 200, 80, 405, 400), ""  ,userTooltipSytle);
            GUI.Label(new Rect(Screen.width / 2 - 170, 100, 60, 25), "FELHASZNÁLÓI FELÜLET" ,szoveg);
////            email = GUI.TextField (new Rect (Screen.width / 2 - 60, 135, 250, 25), email, 40);
////            username = GUI.TextField ( new Rect (Screen.width / 2 - 60, 170, 250, 25), username, 40);
////            password = GUI.PasswordField (new Rect (Screen.width / 2 - 60, 205, 250, 25), password, "*"[0], 40);
////            confirm = GUI.PasswordField (new Rect (Screen.width / 2 - 60, 240, 200, 25), confirm, "*"[0], 40);
//
            GUI.Label(new Rect(Screen.width / 2 - 170, 380, 60, 25), "Hamarosan elérhető!\n\n" +
                     "- árkalkulációt készíthet\n" +
                      "- megrendelését elküldheti\n" +
                      "- felhasználói adatokat módosíthat" ,szoveg);

//            if (GameObject.Find("B1").GetComponent<alapterulet>().counterAT)
//            {
//                //counterAT=0;
//            }
//
//
////            if (GUI.Button (new Rect (Screen.width / 2 - 60, 280, 100, 30), "Belépés")) {
////                Debug.Log(email + " " + username + " " + password + " " + confirm);



         
        //--------------------------------

            GUILayout.BeginArea(new Rect(Screen.width / 2 -170,150,200, 300));
//                GUILayout.BeginHorizontal();
//                GUILayout.FlexibleSpace();
//                GUILayout.BeginVertical();
//                GUILayout.FlexibleSpace();
             
                if(currentMenu == Menu.MainMenu) {
                 
                    GUILayout.Box("Térberendezés");
                    GUILayout.Space(10);
                 
                    if(GUILayout.Button("ÚJ üres tér")) {
                                                         
                        foreach(GameObject AllgameObjectsDelete in GameObject.FindGameObjectsWithTag("Player"))
                        {
                        Destroy (AllgameObjectsDelete);
                        }
                        currentMenu = Menu.MainMenu;


                    }

                    if(GUILayout.Button("Az összeállítás mentése")) {
                        Game.current = new Game();
                        currentMenu = Menu.NewGame;
                    }
                 
                    if(GUILayout.Button("Korábbi betöltése")) {
                        SaveLoad.Load();
                        currentMenu = Menu.Continue;
                    }

//                    if(GUILayout.Button("A lista törlése")) {
//                        SaveLoad.Load();
//                        Game gameToRemove = SaveLoad.savedGames.First<game>; //Replace with game you want to remove
//                        if (gameToRemove != null)
//                        {
//                        SaveLoad.savedGames.Remove(gameToRemove);
//                        }
//                        SaveLoad.Save();
//                        }


                }
             
                else if (currentMenu == Menu.NewGame) {
                 
                    GUILayout.Box("Személyes adatok");
                    GUILayout.Space(10);
                 
                    GUILayout.Label(("Az Ön neve"), szoveg);
                    Game.current.vezetek.name = GUILayout.TextField(Game.current.vezetek.name, 20);
                GUILayout.Label(("Az összeállítás megnevezése"), szoveg);
                    Game.current.osszeallitas.name = GUILayout.TextField(Game.current.osszeallitas.name, 20);

                 
                    if(GUILayout.Button("Mentés")) {
                        //Save the current Game as a new saved Game

     
                     

                        GameObject[]go = GameObject.FindGameObjectsWithTag("Player"); 
                        foreach(GameObject AllgameObjectsSave in go)
                            {
                //            prefabLista.Add (new Game(AllgameObjectsSave.name));
                         
                            }
//                    foreach(GameObject AllgameObjectsSave in GameObject.FindGameObjectsWithTag("Player"))
//                    {
                    //    Game.current.savedPrefabName = AllgameObjectsSave.name;
                    //    prefabLista.Add (new Game(AllgameObjectsSave.name));
//                        prefabLista.Add (new Game(AllgameObjectsSave.transform));
                //        Game.current.savedPrefabName = AllgameObjectsSave.name;
//                        prefabLista.Add (new Game());
//                        Game.current.savedPrefabName.name = AllgameObjectsSave.name;
//                        prefabLista.Add(sPName);
//                        Debug.Log (AllgameObjectsSave.name);




//
//                    }

             


                        SaveLoad.Save();
                        //Move on to game...
                        //Application.LoadLevel(1);
                        currentMenu = Menu.MainMenu;
                    }
                 
                    GUILayout.Space(10);
                    if(GUILayout.Button("Vissza")) {
                        currentMenu = Menu.MainMenu;
                    }
                 
                }
             
                else if (currentMenu == Menu.Continue) {
                 
                    GUILayout.Box("Mentés kiválasztása");
                    GUILayout.Space(10);
                 
                    foreach(Game g in SaveLoad.savedGames) {
                        if(GUILayout.Button(g.vezetek.name + " - " + g.osszeallitas.name)) {
                        foreach(GameObject AllgameObjectsDelete in GameObject.FindGameObjectsWithTag("Player"))
                        {
                            Destroy (AllgameObjectsDelete);
                        } 
                        Game.current = g;
                    //    Instantiate (Resources.Load ("Prefabs/AS-A-8.6-F.E") as GameObject);
                    //    Debug.Log (Game.current.savedPrefabName.name);
                    //    Instantiate (Resources.Load ("Prefabs/"+Game.current.savedPrefabName.name));

                        //GameObject myObj = Instantiate (myObjects [whichItem]) as GameObject;
                            //Move on to game...
                            //Application.LoadLevel(1);
                         
                        }
                     
                    }
                 
                    GUILayout.Space(10);
                    if(GUILayout.Button("Vissza")) {
                        currentMenu = Menu.MainMenu;
                    }
                 
                }
             
//                GUILayout.FlexibleSpace();
//                GUILayout.EndVertical();
//                GUILayout.FlexibleSpace();
//                GUILayout.EndHorizontal();
                GUILayout.EndArea();
             
            }







         
         
             
     
        }
    }

Are all of the names that you want to save being added and stored in prefabLista? If you want to save all those you can create a new PlayerPrefs for each one in prefabLista and save it.

I have made a new PrefabDatabase. Now I can save in this file the names of the object.
I don’t know how can I save these data in the game save file for the corresponding Game.current?

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PrefabDatabase : MonoBehaviour {

    public List<Game> prefabLista = new List<Game>();

    // Use this for initialization
    void Start ()
    {

    }

}
private PrefabDatabase PlayerPrefab;


GameObject[]go = GameObject.FindGameObjectsWithTag("Player");
                        foreach(GameObject AllgameObjectsSave in go)
                            {
                        PlayerPrefab = GameObject.FindGameObjectWithTag("Prefab Database").GetComponent<PrefabDatabase>();
                  //      PlayerPrefab.prefabLista.Add (new Game(AllgameObjectsSave.name));    //This add public elements in to inspector
                        PrefabDatabase.prefabLista.Add (newGame(AllgameObjectsSave.name));  //This add static data
                            }

I have tried to add the prefabLista to the save file:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

public static class SaveLoad {

    public static List<Game> savedGames = new List<Game>();
   
    //it's static so we can call it from anywhere
    public static void Save() {
        SaveLoad.savedGames.Add(Game.current);

//    SaveLoad.savedGames.Add(PrefabDatabase.prefabLista);
//    SaveLoad.savedGames.Add(List<PrefabDatabase.prefabLista>);

      BinaryFormatter bf = new BinaryFormatter();
        //Application.persistentDataPath is a string, so if you wanted you can put that into debug.log if you want to know where save games are located
        FileStream file = File.Create (Application.persistentDataPath + "/savedGames.gd"); //you can call it anything you want
        bf.Serialize(file, SaveLoad.savedGames);
        Debug.Log (Application.persistentDataPath);
        file.Close();
    }

    public static void Load() {
        if(File.Exists(Application.persistentDataPath + "/savedGames.gd")) {
            BinaryFormatter bf = new BinaryFormatter();
            FileStream file = File.Open(Application.persistentDataPath + "/savedGames.gd", FileMode.Open);
            SaveLoad.savedGames = (List<Game>)bf.Deserialize(file);
            file.Close();
        }
    }
}

I dont know how the current state works, but like I said, can you just save each name as a PlayerPrefs string?

Hi,

I hope this video will help you to understand my project.
http://www.csongorfekete.com/download/Prefab_names_to_list.mov

I’m sorry, but I cant quite understand the problem. I dont have much experience with saving using the current state.

Is prefab lista having any trouble collecting all of the names? It might have something to do with the list being of type Game instead of a string.

I will try to describe in some detail. At this point I have a very easy saveload function. This can store only 2 information, username(vezetek) and description(osszeallitas) but they are only one-one strings. I can load this information back. I’d like to add more data to the save file, therefore I have opened this topic. Only what I can,
adding and saving next strings, but I’d like to save lists of found objects names.

In my video you can see, I get the names of found object in the prefabLista, but I don’t know how I can save them.
If I change prefabLista to static, maybe they are strings, but I don’t see them and I don’t know how I can save them.

Any idea will be helpful.
Thanks.

I have solved:slight_smile:

GameObject[]go = GameObject.FindGameObjectsWithTag("Player");
                        foreach(GameObject AllgameObjectsSave in go)
                            {
    
                        Game.current.prefabLista.Add (new Game(AllgameObjectsSave.name));
                        //Debug.Log(Game.current.prefabLista);

                        //Game.current.savedPrefabName = AllgameObjectsSave.name;
                            }

I have opened the savedGames.gd file with TextEditor and I see the names of found objects.
The last name is wrong character encoded. (AS-8.6-2A-4√ú) != (AS-8.6-2A-4Ü)
How can I get well encoded strings?

ˇˇˇˇAssembly-CSharpqSystem.Collections.Generic.List`1[[Game, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]_items_size_versionGame[]    Game
GamevezetekosszeallitassavedPrefabNameprefabLista    Character    CharacterqSystem.Collections.Generic.List`1[[Game, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]
        CharacternameT    1
Game 


AN-Texas

AS-KI.2

AS-A-8.6-F.E

AS-OA-18.12-B-F.E

AS-8.6-2A-4√ú                        Game

I have tried to Log the prefabLista, but I got this: System.Collections.Generic.List`1[Game]
Why I don’t see the names?
This is important, because: I have tried to Load the saved names and I got this:
ArgumentException: The thing you want to instantiate is null.

Instantiate (Resources.Load ("Prefabs/"+Game.current.prefabLista));

The code is good, because I tried with fix name, then this will be instantiate.

What is wrong, when I tried to load the names?
I didn’t use the savedPrefabName and the sPName, but whit this can work prefabLista.
Why?
```csharp
**using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[System.Serializable]
public class Game {

public static Game current;
public Character vezetek;
public Character osszeallitas;
public string savedPrefabName;
//public static List<Game> prefabLista = new List<Game>();
public List<Game> prefabLista = new List<Game>();


public Game ()
{
    vezetek = new Character();
    osszeallitas = new Character();

}

public Game (string sPName)
{
    savedPrefabName = sPName;

}

}**
```

1 Like