- Home /
How to make instantiated buttons permanent in hierarchy?
Hi, I´m trying to add one button whenever something is added to a database. I already did the connection to the DB and put it to add an instantiated button when something is added in it. Here is my code so far
string MyConnection1 = "datasource=...;port=..;username=..;password=..";
string Query1 = "select Name" +
" from ..." +
"ORDER BY ID DESC LIMIT 1;";
MySqlConnection MyConn1 = new MySqlConnection(MyConnection1);
MySqlCommand MyCommand1 = new MySqlCommand(Query1, MyConn1);
MyConn1.Open();
MySqlDataReader readr = MyCommand1.ExecuteReader();
//int Value = MyCommand2.ExecuteNonQuery();
while (readr.Read())
{
nam = Convert.ToString(readr[0]);
Debug.Log("val = " + nam);
}
//MyCommand2.ExecuteNonQuery();
MyConn1.Close();
GameObject newButton = Instantiate(button) as GameObject;
newButton.transform.SetParent(content.transform, false);
The problem is that it only works on runtime. When I stop the game, the buttons dissapear and I want them to be saved in hierarchy. Can anyone help me solve this? Thanks!
try this:
create a empty prefab
while playing drag & drop the button to the prefab
stop playing and drag & drop the prefab with the button to the hierachy
Hi, thanks for the reply This could be a solution but not for what i'm trying to do. I want it to be created and saved automatically, without the user having to do something. Is it possible?
Answer by xxmariofer · May 14, 2019 at 11:08 AM
well thats a hard task, you can use this example for saving objects created at runtime BUT since is using unity editor it wont work in builds just during editor playmode LINK. if you want to use this for builds just save your buttons in some kind of txt or xml that you can parse (or call the DB at the start of the game) and spawn the buttons then, this is also valid for editor (but wont edit unity scene).
yes, like a savegame, or part of the savegame that was created the button, he use a sql database there, so, is probably that he want use the same way, creating the previous buttons dynamically by script when load a save/start the game...
Your answer
Follow this Question
Related Questions
Instantiate script for randomly selecting sound is suddenly creating New Game Object in hierarcy?? 2 Answers
Instantiate a prefab inside a hiearcy 1 Answer
GameObject Instantiate and check if "activeInHierarchy" (HELP) 0 Answers
Is there a way to make clones appear as children in the hierarchy? 3 Answers
Destroying a prefab doesn't remove it from the Hierarchy list 2 Answers