• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
Question by Afrothundera · Sep 17, 2015 at 05:49 PM · c#instantiateobject

Problema al crear objeto por segunda vez. Problem to instantatiate a object for second try.

Disculpen, tengo un problema con mi codigo que tengo que presentar para mi tesis por motivos estudiantiles (siendo redundante) y no he logrado resolverlo que ademas de no entender porque sucede.

La idea de mi programa es hacer un juego estilo trivia, en la cual, de una cierta pregunta se te dan 4 respuestas que solo 1 de ellas es la correcta. Las Preguntas y Respuestas estan en una base de datos por MySQL.

El Problema radica es cuando yo inicio el juego, sale la primera pregunta en texto y las 4 respuestas en botones sin ningun problema, pero cuando respondo y sale la segunda pregunta solo aparece el texto de la pregunta y los botones de las respuestas no se crean. He revisado todo y he colocado un Debug.Log donde se crean los objetos y si realiza la funcion pero no me crea el objeto.

Les paso el codigo espero puedan ayudarme.

I apologize , I have a problem with my code that I have to submit my thesis and have failed to resolve , that in addition to not understand why it happens.

The idea of my program is to make a style trivia game, in which, in a certain question you get 4 answers that only one of them is correct. The questions and answers are in a MySQL database .

The problem is when I start the game , comes the first question in text and 4 responses buttons without any problem, but when I answer , comes the second question, and only the text of the question is appears and the buttons not. I've checked everything and I have placed a debug.log where objects are created and yes , do the work but did not create the object .

I pass the code I hope you can help me

using System; using UnityEngine.UI; using MySql.Data.MySqlClient; using System.Collections; using UnityEngine.EventSystems;

public class PreguntasYRespuestas : MonoBehaviour {

 public Transform Padre;
 public Button boton;
 public bool Inicio;
 public Animator anim;
 public GameObject Paneles;
 public GameObject Empe;
 //EstadoLogin estadologin;
 
 private Button botpan;
 private string source;
 private MySqlConnection Conex;
 private MySqlCommand Comando;
 private MySqlDataReader LeerBD;
 private bool Error;
 private string TextoError;
 private int NumPregunta;
 private string Examen;
 private bool Respondio;
 private int Puntuacion;
 
 // Use this for initialization
 void Start () {
     Inicio = false;
     Respondio = false;
     NumPregunta = 0;
     Puntuacion = 0;
     source = "Server=localhost;" +
         "Database =tesistest;" +
             "User ID = root;" +
             "Pooling=false;" +
             "Passwaord=";
     
     
 }

 void Update(){

     if (Inicio == true) {

         if(Respondio == true){

             Respondio = false;

         }        
     }

 }



 public void Empezar(){
     Inicio = true;
     NumPregunta = EstadoLogin.estadologin.NumeroPregunta;
     Examen = EstadoLogin.estadologin.ExamRealizar;
     Destroy (Empe);
     Conex = new MySqlConnection (source);
     Buscar ();
 }

 public void Buscar (){
     try{
         Conex.Open();
         Comando = Conex.CreateCommand ();
         Comando.CommandText = "SELECT * FROM `examenes` WHERE `Examenes` like '"+Examen+"' AND `Numero de pregunta` = "+NumPregunta+"";
         LeerBD = Comando.ExecuteReader ();
         //Vector3 Spawnpos = new Vector3(-0.1f,3.5f,0);
         while (LeerBD.Read()) {
             string Pregunta = (string)LeerBD["Pregunta"];
             string Respuesta1 = (string)LeerBD["Resp"];
             string Respuesta2 = (string)LeerBD["Resp 2"];
             string Respuesta3 = (string)LeerBD["Resp 3"];
             string RespuestaCorrecta = (string)LeerBD["RespCorrect"];
             Button BotonResp;

             GameObject PanelDePreguntas = (GameObject)Instantiate (Paneles,Padre.position,Quaternion.identity);
             anim = PanelDePreguntas.GetComponent<Animator>();
             PanelDePreguntas.transform.SetParent (Padre);
             PanelDePreguntas.GetComponent<RectTransform>().sizeDelta = new Vector2(238.51f,190.52f);
             //PanelDePreguntas.transform.localPosition = new Vector3(75.75002f,57.25f,0f);
             PanelDePreguntas.transform.localScale = new Vector3 (1f, 1f, 1f);
             PanelDePreguntas.GetComponentInChildren <Text>().text = Pregunta;
             Transform PanelRespuestas = GameObject.Find("Panel de respuestas").GetComponentInChildren<Text>().transform;

             // las posiciones son : 1)71.95f 2) 26.95 3) -16.5 4) -61.5
             int[] posicion = new int[5];
             posicion[0] = 0;
             posicion[1] = 0;
             posicion[2] = 0;
             posicion[3] = 0;
             posicion[4] = UnityEngine.Random.Range(1,5);
                 if(Respuesta1 != ""){
                 Debug.Log("alo");
                     BotonResp= (Button)Instantiate(boton,PanelRespuestas.transform.position,Quaternion.identity);
                     BotonResp.transform.SetParent(PanelRespuestas);
                     BotonResp.GetComponent<RectTransform>().sizeDelta = new Vector2(182.1f,45f);
                     posicion[0] = posicion[4];
                     Posicionar(BotonResp,posicion[0]);
                     BotonResp.transform.localScale = new Vector3(1f,1f,1f);
                     BotonResp.GetComponentInChildren<Text>().text = Respuesta1;
                     BotonResp.onClick.AddListener(()=>Fallar(anim,PanelDePreguntas));
                     posicion[4] = UnityEngine.Random.Range(1,5);
                 Debug.Log("Objeto creado "+NumPregunta);
                 }
                 
                 if(Respuesta2 != ""){
                     BotonResp = (Button)Instantiate(boton,PanelRespuestas.transform.position,Quaternion.identity);
                     BotonResp.transform.SetParent(PanelRespuestas);
                     BotonResp.GetComponent<RectTransform>().sizeDelta = new Vector2(182.1f,45f);
                     posicion[1] = posicion[4];
                     while(posicion[1] == posicion[0]) posicion[1] = UnityEngine.Random.Range(1,5);
                     Posicionar(BotonResp,posicion[1]);
                     BotonResp.transform.localScale = new Vector3(1f,1f,1f);
                     BotonResp.GetComponentInChildren<Text>().text = Respuesta2;
                     BotonResp.onClick.AddListener(()=>Fallar(anim,PanelDePreguntas));
                     posicion[4] = UnityEngine.Random.Range(1,5);
                 Debug.Log("1 Objeto creado "+NumPregunta);
                 }
                 if(Respuesta3 != ""){
                     BotonResp= (Button)Instantiate(boton,PanelRespuestas.transform.position,Quaternion.identity);
                     BotonResp.transform.SetParent(PanelRespuestas);
                     BotonResp.GetComponent<RectTransform>().sizeDelta = new Vector2(182.1f,45f);
                     posicion[2] = posicion[4];
                     while(posicion[2] == posicion[0] || posicion[2] == posicion[1]) 
                     posicion[2] = UnityEngine.Random.Range(1,5);
                     Posicionar(BotonResp,posicion[2]);
                     BotonResp.transform.localScale = new Vector3(1f,1f,1f);
                     BotonResp.GetComponentInChildren<Text>().text = Respuesta3;
                     BotonResp.onClick.AddListener(()=>Fallar(anim,PanelDePreguntas));
                     posicion[4] = UnityEngine.Random.Range(1,5);
                 Debug.Log("2 Objeto creado "+NumPregunta);
                 }
                 if(RespuestaCorrecta != ""){
                     BotonResp= (Button)Instantiate(boton,PanelRespuestas.transform.position,Quaternion.identity);
                     BotonResp.transform.SetParent(PanelRespuestas);
                     BotonResp.GetComponent<RectTransform>().sizeDelta = new Vector2(182.1f,45f);
                     posicion[3] = posicion[4];
                     while(posicion[3] == posicion[0] || posicion[3] == posicion[1] || posicion[3] == posicion[2]) 
                     posicion[3] = UnityEngine.Random.Range(1,5);
                     Posicionar(BotonResp,posicion[3]);
                     BotonResp.transform.localScale = new Vector3(1f,1f,1f);
                     BotonResp.GetComponentInChildren<Text>().text = RespuestaCorrecta;
                     BotonResp.onClick.AddListener(()=>Acertar(anim,PanelDePreguntas));
                 Debug.Log("3 Objeto creado "+NumPregunta);
                 }
             
             Debug.Log(Pregunta);
             Debug.Log(Respuesta1);
             Debug.Log(Respuesta2);
             Debug.Log(Respuesta3);
             Debug.Log(RespuestaCorrecta);

         }

     }
     catch(Exception ex){
         Error = true;
         TextoError = ex.ToString();    
     }
     
     Conex.Close ();
     Debug.Log("cerrado");

 }

 void Posicionar(Button boton, int Posi){
     
         if (Posi == 1)
             boton.transform.localPosition = new Vector3 (-11.05f, 71.95f, 0);
         if (Posi == 2)
             boton.transform.localPosition = new Vector3 (-11.05f, 26.95f, 0);
         if (Posi == 3)
             boton.transform.localPosition = new Vector3 (-11.05f, -16.5f, 0);
         if (Posi == 4)
             boton.transform.localPosition = new Vector3 (-11.05f, -61.5f, 0);

 } 

 public void Fallar(Animator fallo, GameObject Eliminar){
     fallo.SetBool ("Cerrar",true);

     Destroy (Eliminar);
     Respondio = true;
     NumPregunta += 1;
     EstadoLogin.estadologin.NumeroPregunta = NumPregunta;

     Buscar();
 }



 public void Acertar(Animator acierto, GameObject Eliminar){
     acierto.SetBool ("Cerrar",true);
     ;
     Destroy (Eliminar);
     Respondio = true;
     NumPregunta += 1;
     EstadoLogin.estadologin.NumeroPregunta = NumPregunta;
     Puntuacion += 1;

     Buscar();
 }

}

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image

Answer by Afrothundera · Sep 17, 2015 at 05:53 PM

 using System;
 using UnityEngine.UI;
 using MySql.Data.MySqlClient;
 using System.Collections;
 using UnityEngine.EventSystems;
 
 public class PreguntasYRespuestas : MonoBehaviour {
 
     public Transform Padre;
     public Button boton;
     public bool Inicio;
     public Animator anim;
     public GameObject Paneles;
     public GameObject Empe;
     //EstadoLogin estadologin;
     
     private Button botpan;
     private string source;
     private MySqlConnection Conex;
     private MySqlCommand Comando;
     private MySqlDataReader LeerBD;
     private bool Error;
     private string TextoError;
     private int NumPregunta;
     private string Examen;
     private bool Respondio;
     private int Puntuacion;
     
     // Use this for initialization
     void Start () {
         Inicio = false;
         Respondio = false;
         NumPregunta = 0;
         Puntuacion = 0;
         source = "Server=localhost;" +
             "Database =tesistest;" +
                 "User ID = root;" +
                 "Pooling=false;" +
                 "Passwaord=";
         
         
     }
 
     void Update(){
 
         if (Inicio == true) {
 
             if(Respondio == true){
 
                 Respondio = false;
 
             }        
         }
 
     }
 
 
 
     public void Empezar(){
         Inicio = true;
         NumPregunta = EstadoLogin.estadologin.NumeroPregunta;
         Examen = EstadoLogin.estadologin.ExamRealizar;
         Destroy (Empe);
         Conex = new MySqlConnection (source);
         Buscar ();
     }
 
     public void Buscar (){
         try{
             Conex.Open();
             Comando = Conex.CreateCommand ();
             Comando.CommandText = "SELECT * FROM `examenes` WHERE `Examenes` like '"+Examen+"' AND `Numero de pregunta` = "+NumPregunta+"";
             LeerBD = Comando.ExecuteReader ();
             //Vector3 Spawnpos = new Vector3(-0.1f,3.5f,0);
             while (LeerBD.Read()) {
                 string Pregunta = (string)LeerBD["Pregunta"];
                 string Respuesta1 = (string)LeerBD["Resp"];
                 string Respuesta2 = (string)LeerBD["Resp 2"];
                 string Respuesta3 = (string)LeerBD["Resp 3"];
                 string RespuestaCorrecta = (string)LeerBD["RespCorrect"];
                 Button BotonResp;
 
                 GameObject PanelDePreguntas = (GameObject)Instantiate (Paneles,Padre.position,Quaternion.identity);
                 anim = PanelDePreguntas.GetComponent<Animator>();
                 PanelDePreguntas.transform.SetParent (Padre);
                 PanelDePreguntas.GetComponent<RectTransform>().sizeDelta = new Vector2(238.51f,190.52f);
                 //PanelDePreguntas.transform.localPosition = new Vector3(75.75002f,57.25f,0f);
                 PanelDePreguntas.transform.localScale = new Vector3 (1f, 1f, 1f);
                 PanelDePreguntas.GetComponentInChildren <Text>().text = Pregunta;
                 Transform PanelRespuestas = GameObject.Find("Panel de respuestas").GetComponentInChildren<Text>().transform;
 
                 // las posiciones son : 1)71.95f 2) 26.95 3) -16.5 4) -61.5
                 int[] posicion = new int[5];
                 posicion[0] = 0;
                 posicion[1] = 0;
                 posicion[2] = 0;
                 posicion[3] = 0;
                 posicion[4] = UnityEngine.Random.Range(1,5);
                     if(Respuesta1 != ""){
                     Debug.Log("alo");
                         BotonResp= (Button)Instantiate(boton,PanelRespuestas.transform.position,Quaternion.identity);
                         BotonResp.transform.SetParent(PanelRespuestas);
                         BotonResp.GetComponent<RectTransform>().sizeDelta = new Vector2(182.1f,45f);
                         posicion[0] = posicion[4];
                         Posicionar(BotonResp,posicion[0]);
                         BotonResp.transform.localScale = new Vector3(1f,1f,1f);
                         BotonResp.GetComponentInChildren<Text>().text = Respuesta1;
                         BotonResp.onClick.AddListener(()=>Fallar(anim,PanelDePreguntas));
                         posicion[4] = UnityEngine.Random.Range(1,5);
                     Debug.Log("Objeto creado "+NumPregunta);
                     }
                     
                     if(Respuesta2 != ""){
                         BotonResp = (Button)Instantiate(boton,PanelRespuestas.transform.position,Quaternion.identity);
                         BotonResp.transform.SetParent(PanelRespuestas);
                         BotonResp.GetComponent<RectTransform>().sizeDelta = new Vector2(182.1f,45f);
                         posicion[1] = posicion[4];
                         while(posicion[1] == posicion[0]) posicion[1] = UnityEngine.Random.Range(1,5);
                         Posicionar(BotonResp,posicion[1]);
                         BotonResp.transform.localScale = new Vector3(1f,1f,1f);
                         BotonResp.GetComponentInChildren<Text>().text = Respuesta2;
                         BotonResp.onClick.AddListener(()=>Fallar(anim,PanelDePreguntas));
                         posicion[4] = UnityEngine.Random.Range(1,5);
                     Debug.Log("1 Objeto creado "+NumPregunta);
                     }
                     if(Respuesta3 != ""){
                         BotonResp= (Button)Instantiate(boton,PanelRespuestas.transform.position,Quaternion.identity);
                         BotonResp.transform.SetParent(PanelRespuestas);
                         BotonResp.GetComponent<RectTransform>().sizeDelta = new Vector2(182.1f,45f);
                         posicion[2] = posicion[4];
                         while(posicion[2] == posicion[0] || posicion[2] == posicion[1]) 
                         posicion[2] = UnityEngine.Random.Range(1,5);
                         Posicionar(BotonResp,posicion[2]);
                         BotonResp.transform.localScale = new Vector3(1f,1f,1f);
                         BotonResp.GetComponentInChildren<Text>().text = Respuesta3;
                         BotonResp.onClick.AddListener(()=>Fallar(anim,PanelDePreguntas));
                         posicion[4] = UnityEngine.Random.Range(1,5);
                     Debug.Log("2 Objeto creado "+NumPregunta);
                     }
                     if(RespuestaCorrecta != ""){
                         BotonResp= (Button)Instantiate(boton,PanelRespuestas.transform.position,Quaternion.identity);
                         BotonResp.transform.SetParent(PanelRespuestas);
                         BotonResp.GetComponent<RectTransform>().sizeDelta = new Vector2(182.1f,45f);
                         posicion[3] = posicion[4];
                         while(posicion[3] == posicion[0] || posicion[3] == posicion[1] || posicion[3] == posicion[2]) 
                         posicion[3] = UnityEngine.Random.Range(1,5);
                         Posicionar(BotonResp,posicion[3]);
                         BotonResp.transform.localScale = new Vector3(1f,1f,1f);
                         BotonResp.GetComponentInChildren<Text>().text = RespuestaCorrecta;
                         BotonResp.onClick.AddListener(()=>Acertar(anim,PanelDePreguntas));
                     Debug.Log("3 Objeto creado "+NumPregunta);
                     }
                 
                 Debug.Log(Pregunta);
                 Debug.Log(Respuesta1);
                 Debug.Log(Respuesta2);
                 Debug.Log(Respuesta3);
                 Debug.Log(RespuestaCorrecta);
 
             }
 
         }
         catch(Exception ex){
             Error = true;
             TextoError = ex.ToString();    
         }
         
         Conex.Close ();
         Debug.Log("cerrado");
 
     }
 
     void Posicionar(Button boton, int Posi){
         
             if (Posi == 1)
                 boton.transform.localPosition = new Vector3 (-11.05f, 71.95f, 0);
             if (Posi == 2)
                 boton.transform.localPosition = new Vector3 (-11.05f, 26.95f, 0);
             if (Posi == 3)
                 boton.transform.localPosition = new Vector3 (-11.05f, -16.5f, 0);
             if (Posi == 4)
                 boton.transform.localPosition = new Vector3 (-11.05f, -61.5f, 0);
 
     } 
 
     public void Fallar(Animator fallo, GameObject Eliminar){
         fallo.SetBool ("Cerrar",true);
 
         Destroy (Eliminar);
         Respondio = true;
         NumPregunta += 1;
         EstadoLogin.estadologin.NumeroPregunta = NumPregunta;
 
         Buscar();
     }
 
 
 
     public void Acertar(Animator acierto, GameObject Eliminar){
         acierto.SetBool ("Cerrar",true);
         ;
         Destroy (Eliminar);
         Respondio = true;
         NumPregunta += 1;
         EstadoLogin.estadologin.NumeroPregunta = NumPregunta;
         Puntuacion += 1;
 
         Buscar();
     }
 
 
 }

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Object reference not set message, directly after instantiation 1 Answer

How do you pick up an instantiated object? 1 Answer

*Solved* Loop instantiating objects causes my Unity Editor to crash 1 Answer

Instantiate object (on touch position) by touch on the collider of other object 1 Answer

Ideas for farming? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges