Search Unity

How to add names of FindGameObjectsWithTag to list?

Discussion in 'Scripting' started by Slyrfecso1, Aug 30, 2015.

  1. Slyrfecso1

    Slyrfecso1

    Joined:
    Jul 16, 2012
    Posts:
    100
    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?


    Code (CSharp):
    1. foreach(GameObject AllgameObjectsSave in GameObject.FindGameObjectsWithTag("Player"))
    2.                     {
    3.                     Game.current.savedPrefabName.name = AllgameObjectsSave.name;
    4.                     Debug.Log (AllgameObjectsSave.name);
    5.                     }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [System.Serializable]
    5. public class Game {
    6.  
    7.     public static Game current;
    8.     public Character vezetek;
    9.     public Character osszeallitas;
    10.     public Character savedPrefabName;
    11.  
    12.  
    13.  
    14.     public Game () {
    15.         vezetek = new Character();
    16.         osszeallitas = new Character();
    17.         savedPrefabName = new Character();
    18.     }
    19.    
    20. }
    21.  
     
    Last edited: Aug 30, 2015
  2. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    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.
     
  3. Slyrfecso1

    Slyrfecso1

    Joined:
    Jul 16, 2012
    Posts:
    100
    You have right.
    I have added at the top:
    publicList<Game> prefabLista = newList<Game>();

    Code (CSharp):
    1. foreach(GameObject AllgameObjectsSave in GameObject.FindGameObjectsWithTag("Player"))
    2. {
    3. prefabLista.Add (new Game(AllgameObjectsSave.name));
    4. }

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [System.Serializable]
    5. public class Game {
    6.  
    7.     public static Game current;
    8.     public Character vezetek;
    9.     public Character osszeallitas;
    10.     public string savedPrefabName;
    11.  
    12.  
    13.  
    14.     public Game ()
    15.     {
    16.         vezetek = new Character();
    17.         osszeallitas = new Character();
    18.     }
    19.  
    20.     public Game (string sPName)
    21.     {
    22.         savedPrefabName = sPName;
    23.     }
    24.  
    25. }
    26.  
    How can I save them with "Game.current."?
     
    Last edited: Aug 30, 2015
  4. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    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.
     
    Kiwasi likes this.
  5. Slyrfecso1

    Slyrfecso1

    Joined:
    Jul 16, 2012
    Posts:
    100
    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.



    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class felhasznalo : MonoBehaviour {
    6.  
    7.     public GUIStyle userStyel;
    8.     public GUIStyle userTooltipSytle;
    9.     public GUIStyle szoveg;
    10.     int counterAT=0;
    11.  
    12. //    string email = "";
    13. //    string username = "";
    14. //    string password = "";
    15. //    string confirm = "";
    16.  
    17.     public enum Menu {
    18.         MainMenu,
    19.         NewGame,
    20.         Continue
    21.     }
    22.  
    23.     public Menu currentMenu;
    24.     public List<Game> prefabLista = new List<Game>();
    25.  
    26.  
    27.  
    28. //    public void AddTarget(Transform savedPrefabTransform){
    29. //        prefabLista.Add (new Game());
    30. //    
    31. //    }
    32.  
    33.  
    34.  
    35.     void OnGUI () {
    36.      
    37.  
    38.         if (counterAT==2)
    39.         {
    40.             counterAT = counterAT-2;
    41.         }
    42.      
    43.         if (GUI.Button (new Rect (Screen.width/2+35, 10, 50, 48), "" ,userStyel))
    44.         {
    45.             counterAT++;
    46.             Debug.Log(counterAT);
    47.         }
    48.      
    49.      
    50.         if (counterAT == 1)
    51.         {
    52.         GUI.Box (new Rect (Screen.width / 2 - 200, 80, 405, 400), ""  ,userTooltipSytle);
    53.             GUI.Label(new Rect(Screen.width / 2 - 170, 100, 60, 25), "FELHASZNÁLÓI FELÜLET" ,szoveg);
    54. ////            email = GUI.TextField (new Rect (Screen.width / 2 - 60, 135, 250, 25), email, 40);
    55. ////            username = GUI.TextField ( new Rect (Screen.width / 2 - 60, 170, 250, 25), username, 40);
    56. ////            password = GUI.PasswordField (new Rect (Screen.width / 2 - 60, 205, 250, 25), password, "*"[0], 40);
    57. ////            confirm = GUI.PasswordField (new Rect (Screen.width / 2 - 60, 240, 200, 25), confirm, "*"[0], 40);
    58. //
    59.             GUI.Label(new Rect(Screen.width / 2 - 170, 380, 60, 25), "Hamarosan elérhető!\n\n" +
    60.                      "- árkalkulációt készíthet\n" +
    61.                       "- megrendelését elküldheti\n" +
    62.                       "- felhasználói adatokat módosíthat" ,szoveg);
    63.  
    64. //            if (GameObject.Find("B1").GetComponent<alapterulet>().counterAT)
    65. //            {
    66. //                //counterAT=0;
    67. //            }
    68. //
    69. //
    70. ////            if (GUI.Button (new Rect (Screen.width / 2 - 60, 280, 100, 30), "Belépés")) {
    71. ////                Debug.Log(email + " " + username + " " + password + " " + confirm);
    72.  
    73.  
    74.  
    75.          
    76.         //--------------------------------
    77.  
    78.             GUILayout.BeginArea(new Rect(Screen.width / 2 -170,150,200, 300));
    79. //                GUILayout.BeginHorizontal();
    80. //                GUILayout.FlexibleSpace();
    81. //                GUILayout.BeginVertical();
    82. //                GUILayout.FlexibleSpace();
    83.              
    84.                 if(currentMenu == Menu.MainMenu) {
    85.                  
    86.                     GUILayout.Box("Térberendezés");
    87.                     GUILayout.Space(10);
    88.                  
    89.                     if(GUILayout.Button("ÚJ üres tér")) {
    90.                                                          
    91.                         foreach(GameObject AllgameObjectsDelete in GameObject.FindGameObjectsWithTag("Player"))
    92.                         {
    93.                         Destroy (AllgameObjectsDelete);
    94.                         }
    95.                         currentMenu = Menu.MainMenu;
    96.  
    97.  
    98.                     }
    99.  
    100.                     if(GUILayout.Button("Az összeállítás mentése")) {
    101.                         Game.current = new Game();
    102.                         currentMenu = Menu.NewGame;
    103.                     }
    104.                  
    105.                     if(GUILayout.Button("Korábbi betöltése")) {
    106.                         SaveLoad.Load();
    107.                         currentMenu = Menu.Continue;
    108.                     }
    109.  
    110. //                    if(GUILayout.Button("A lista törlése")) {
    111. //                        SaveLoad.Load();
    112. //                        Game gameToRemove = SaveLoad.savedGames.First<game>; //Replace with game you want to remove
    113. //                        if (gameToRemove != null)
    114. //                        {
    115. //                        SaveLoad.savedGames.Remove(gameToRemove);
    116. //                        }
    117. //                        SaveLoad.Save();
    118. //                        }
    119.  
    120.  
    121.                 }
    122.              
    123.                 else if (currentMenu == Menu.NewGame) {
    124.                  
    125.                     GUILayout.Box("Személyes adatok");
    126.                     GUILayout.Space(10);
    127.                  
    128.                     GUILayout.Label(("Az Ön neve"), szoveg);
    129.                     Game.current.vezetek.name = GUILayout.TextField(Game.current.vezetek.name, 20);
    130.                 GUILayout.Label(("Az összeállítás megnevezése"), szoveg);
    131.                     Game.current.osszeallitas.name = GUILayout.TextField(Game.current.osszeallitas.name, 20);
    132.  
    133.                  
    134.                     if(GUILayout.Button("Mentés")) {
    135.                         //Save the current Game as a new saved Game
    136.  
    137.      
    138.                      
    139.  
    140.                         GameObject[]go = GameObject.FindGameObjectsWithTag("Player");
    141.                         foreach(GameObject AllgameObjectsSave in go)
    142.                             {
    143.                 //            prefabLista.Add (new Game(AllgameObjectsSave.name));
    144.                          
    145.                             }
    146. //                    foreach(GameObject AllgameObjectsSave in GameObject.FindGameObjectsWithTag("Player"))
    147. //                    {
    148.                     //    Game.current.savedPrefabName = AllgameObjectsSave.name;
    149.                     //    prefabLista.Add (new Game(AllgameObjectsSave.name));
    150. //                        prefabLista.Add (new Game(AllgameObjectsSave.transform));
    151.                 //        Game.current.savedPrefabName = AllgameObjectsSave.name;
    152. //                        prefabLista.Add (new Game());
    153. //                        Game.current.savedPrefabName.name = AllgameObjectsSave.name;
    154. //                        prefabLista.Add(sPName);
    155. //                        Debug.Log (AllgameObjectsSave.name);
    156.  
    157.  
    158.  
    159.  
    160. //
    161. //                    }
    162.  
    163.              
    164.  
    165.  
    166.                         SaveLoad.Save();
    167.                         //Move on to game...
    168.                         //Application.LoadLevel(1);
    169.                         currentMenu = Menu.MainMenu;
    170.                     }
    171.                  
    172.                     GUILayout.Space(10);
    173.                     if(GUILayout.Button("Vissza")) {
    174.                         currentMenu = Menu.MainMenu;
    175.                     }
    176.                  
    177.                 }
    178.              
    179.                 else if (currentMenu == Menu.Continue) {
    180.                  
    181.                     GUILayout.Box("Mentés kiválasztása");
    182.                     GUILayout.Space(10);
    183.                  
    184.                     foreach(Game g in SaveLoad.savedGames) {
    185.                         if(GUILayout.Button(g.vezetek.name + " - " + g.osszeallitas.name)) {
    186.                         foreach(GameObject AllgameObjectsDelete in GameObject.FindGameObjectsWithTag("Player"))
    187.                         {
    188.                             Destroy (AllgameObjectsDelete);
    189.                         }
    190.                         Game.current = g;
    191.                     //    Instantiate (Resources.Load ("Prefabs/AS-A-8.6-F.E") as GameObject);
    192.                     //    Debug.Log (Game.current.savedPrefabName.name);
    193.                     //    Instantiate (Resources.Load ("Prefabs/"+Game.current.savedPrefabName.name));
    194.  
    195.                         //GameObject myObj = Instantiate (myObjects [whichItem]) as GameObject;
    196.                             //Move on to game...
    197.                             //Application.LoadLevel(1);
    198.                          
    199.                         }
    200.                      
    201.                     }
    202.                  
    203.                     GUILayout.Space(10);
    204.                     if(GUILayout.Button("Vissza")) {
    205.                         currentMenu = Menu.MainMenu;
    206.                     }
    207.                  
    208.                 }
    209.              
    210. //                GUILayout.FlexibleSpace();
    211. //                GUILayout.EndVertical();
    212. //                GUILayout.FlexibleSpace();
    213. //                GUILayout.EndHorizontal();
    214.                 GUILayout.EndArea();
    215.              
    216.             }
    217.  
    218.  
    219.  
    220.  
    221.  
    222.  
    223.  
    224.          
    225.          
    226.              
    227.      
    228.         }
    229.     }
    230.  
     
    Last edited: Nov 8, 2015
  6. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    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.
     
  7. Slyrfecso1

    Slyrfecso1

    Joined:
    Jul 16, 2012
    Posts:
    100
    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?


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class PrefabDatabase : MonoBehaviour {
    6.  
    7.     public List<Game> prefabLista = new List<Game>();
    8.  
    9.     // Use this for initialization
    10.     void Start ()
    11.     {
    12.  
    13.     }
    14.  
    15. }
    16.  


    Code (CSharp):
    1.  
    2. private PrefabDatabase PlayerPrefab;
    3.  
    4.  
    5. GameObject[]go = GameObject.FindGameObjectsWithTag("Player");
    6.                         foreach(GameObject AllgameObjectsSave in go)
    7.                             {
    8.                         PlayerPrefab = GameObject.FindGameObjectWithTag("Prefab Database").GetComponent<PrefabDatabase>();
    9.                   //      PlayerPrefab.prefabLista.Add (new Game(AllgameObjectsSave.name));    //This add public elements in to inspector
    10.                         PrefabDatabase.prefabLista.Add (newGame(AllgameObjectsSave.name));  //This add static data
    11.                             }


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

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Runtime.Serialization.Formatters.Binary;
    5. using System.IO;
    6.  
    7. public static class SaveLoad {
    8.  
    9.     public static List<Game> savedGames = new List<Game>();
    10.    
    11.     //it's static so we can call it from anywhere
    12.     public static void Save() {
    13.         SaveLoad.savedGames.Add(Game.current);
    14.  
    15. //    SaveLoad.savedGames.Add(PrefabDatabase.prefabLista);
    16. //    SaveLoad.savedGames.Add(List<PrefabDatabase.prefabLista>);
    17.  
    18.       BinaryFormatter bf = new BinaryFormatter();
    19.         //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
    20.         FileStream file = File.Create (Application.persistentDataPath + "/savedGames.gd"); //you can call it anything you want
    21.         bf.Serialize(file, SaveLoad.savedGames);
    22.         Debug.Log (Application.persistentDataPath);
    23.         file.Close();
    24.     }
    25.  
    26.     public static void Load() {
    27.         if(File.Exists(Application.persistentDataPath + "/savedGames.gd")) {
    28.             BinaryFormatter bf = new BinaryFormatter();
    29.             FileStream file = File.Open(Application.persistentDataPath + "/savedGames.gd", FileMode.Open);
    30.             SaveLoad.savedGames = (List<Game>)bf.Deserialize(file);
    31.             file.Close();
    32.         }
    33.     }
    34. }
    35.  
    36.  
     
    Last edited: Sep 1, 2015
  8. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    I dont know how the current state works, but like I said, can you just save each name as a PlayerPrefs string?
     
  9. Slyrfecso1

    Slyrfecso1

    Joined:
    Jul 16, 2012
    Posts:
    100
  10. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    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.
     
  11. Slyrfecso1

    Slyrfecso1

    Joined:
    Jul 16, 2012
    Posts:
    100
    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.
     
  12. Slyrfecso1

    Slyrfecso1

    Joined:
    Jul 16, 2012
    Posts:
    100
    I have solved:)

    Code (CSharp):
    1. GameObject[]go = GameObject.FindGameObjectsWithTag("Player");
    2.                         foreach(GameObject AllgameObjectsSave in go)
    3.                             {
    4.    
    5.                         Game.current.prefabLista.Add (new Game(AllgameObjectsSave.name));
    6.                         //Debug.Log(Game.current.prefabLista);
    7.  
    8.                         //Game.current.savedPrefabName = AllgameObjectsSave.name;
    9.                             }

    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?

    Code (CSharp):
    1. ˇˇˇˇAssembly-CSharpqSystem.Collections.Generic.List`1[[Game, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]_items_size_versionGame[]    Game
    2. GamevezetekosszeallitassavedPrefabNameprefabLista    Character    CharacterqSystem.Collections.Generic.List`1[[Game, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]
    3.         CharacternameT    1
    4. Game
    5.  
    6.  
    7. AN-Texas
    8.  
    9. AS-KI.2
    10.  
    11. AS-A-8.6-F.E
    12.  
    13. AS-OA-18.12-B-F.E
    14.  
    15. 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.

    Code (CSharp):
    1. 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?


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5.  
    6. [System.Serializable]
    7. public class Game {
    8.  
    9.     public static Game current;
    10.     public Character vezetek;
    11.     public Character osszeallitas;
    12.     public string savedPrefabName;
    13.     //public static List<Game> prefabLista = new List<Game>();
    14.     public List<Game> prefabLista = new List<Game>();
    15.  
    16.  
    17.     public Game ()
    18.     {
    19.         vezetek = new Character();
    20.         osszeallitas = new Character();
    21.  
    22.     }
    23.  
    24.     public Game (string sPName)
    25.     {
    26.         savedPrefabName = sPName;
    27.  
    28.     }
    29.  
    30. }

     
    Last edited: Sep 3, 2015
    roger0 likes this.