- Home /
Unity button desabling not working on Android device
I am using Unity for the first time.
I am making a app to work on Android platform.
I made a script to add to one button. The interface has a text input field to add names of a group. After writing some name and hit one button (adicionar), it should add the name in a text field (named textoGrupo). It would also enabled a second button (named seguinte), that would be disabled since the begining of the script execution. I also have another text field (textoElementoRepetido) that should have text only if the name written is repeated.
It works fine when I tested it in Unity. But when I test it on my Android device it does nothing of what's on the script.
Here is my code:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.Data; using Mono.Data.SqliteClient; using TMPro; using System.IO;
public class grupo : MonoBehaviour { List elementos = new List(); private IDbConnection connection; private IDbCommand command; string dbFile;
int idGrupo = 0;
public TMP_InputField input;
public TMP_Text textoGrupo;
public TMP_Text textoElementoRepetido;
public Button seguinte;
// Start is called before the first frame update
IEnumerator Start()
{
// Ligação à base de dados
string filepath = Application.persistentDataPath + "/TVdatabase.db";
if (!File.Exists(filepath))
{
// if it doesn't ->
// open StreamingAssets directory and load the db ->
WWW loadDB = new WWW("jar:file://" + Application.dataPath + "!/assets/TVdatabase.db"); // this is the path to your StreamingAssets in android
while (!loadDB.isDone)
{
yield return new WaitForSeconds(30);
} // CAREFUL here, for safety reasons you shouldn't let this while loop unattended, place a timer and error check
// then save to Application.persistentDataPath
File.WriteAllBytes(filepath, loadDB.bytes);
}
//open db connection
connection = new SqliteConnection("URI=file:" + filepath);
seguinte.interactable = false;
}
public void AdicionaElemento( )
{
if (input.text != null && input.text != "")
{
string sql;
//se ainda não existirem elementos no grupo, é criado na base de dados um grupo e o seu id é guardado
if (elementos.Count == 0)
{
command = connection.CreateCommand();
connection.Open();
sql = "INSERT INTO Grupo(idAvatar) VALUES(1);";
command.CommandText = sql;
command.ExecuteNonQuery();
sql = "SELECT MAX(idGrupo) FROM GRUPO;";
command.CommandText = sql;
IDataReader reader = command.ExecuteReader();
while (reader.Read())
{
idGrupo = reader.GetInt32(0);
}
connection.Close();
}
string elemento = input.text.ToString();
if (!elementos.Contains(item: elemento))
{
textoElementoRepetido.text = "";
command = connection.CreateCommand();
connection.Open();
sql = "INSERT INTO Elemento(nomeElemento, idGrupo) VALUES('" + elemento + "', " + idGrupo + ");";
command.CommandText = sql;
command.ExecuteNonQuery();
connection.Close();
if (elementos.Count == 0)
{
textoGrupo.text += elemento;
}
else
{
textoGrupo.text += ", " + elemento;
}
elementos.Add(elemento);
seguinte.interactable = true;
input.text = "";
}
else
{
textoElementoRepetido.text = "Essa pessoa já faz parte do teu grupo!";
}
}
else{
seguinte.interactable = false;
}
}
// Update is called once per frame
void Update()
{
}
}
I hope I wrote enough to understand the purpose of this screen. Here is what I attached to the button adicionar.
attached on button
If first tried it on the onClick() event. Didn't work. So I tried it on a trigger. Same result.
I have no idea why this works on unity editor but not on my Android device and I really need to make this work in some time really soon. Can someone please help me?
Update I now know that the problem is somewhere in the data base connection. Because the script works fine when I press the button Adicionar without anything on the text input field.
I also have this question here with more updates.
Answer by misher · Jan 30, 2019 at 08:45 AM
seguinte.interactable = false;
is never reached.
There should be an exception happened before that line of code. Use Android studio logcat tool to find the problem.