2013-12-18 61 views
1

我在Unity中制作了一个文字游戏,其中有很多GameObjects。每个GameObject名称是从A到Z的一个字母。unity3d消失Gameobject

当我单击该对象时,该字母出现在屏幕上。当我写了一个字时,它会将它与字典进行比较,看它是否正确。我想,当我从这些字母中选出一个正确的单词并单击该按钮时,GameObject就会包含这些字母,这些字母就是我所做的单词消失的。

下面是代码:

internal void ReadStudent(string filetoread,string tableName, string itemToSelect, string wCol, string wPar, string wValue){ 

    //jsScript = Camera.main.GetComponent<chk1>(); 

    string connection = "Driver={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=" + filetoread; 
    Debug.Log(connection); 
    string sqlQuery = "SELECT word FROM "+ tableName + " WHERE " + wCol + " " + wPar + " '" + wValue + "'"; 

    OdbcConnection con = new OdbcConnection(connection); 
    OdbcCommand cmd = new OdbcCommand(sqlQuery,con); 
    DataTable dt = new DataTable("dic"); 


    try{ 
     con.Open(); 
     OdbcDataReader reader = cmd.ExecuteReader(); 
     dt.Load(reader); 
     reader.Close(); 
     con.Close(); 
    } 
    catch (Exception ex){ 
     //text = dt.Rows[3][1].ToString(); 
     Debug.Log(ex.ToString()); 
    } 

    finally{ 
     if (con.State!=ConnectionState.Closed){ 
      con.Close(); 
     } 
     con.Dispose(); 
    } 

    if (dt.Rows.Count>0){ 
     text = dt.Rows[0]["word"].ToString(); 
     Debug.Log(text); 
     Debug.Log (wValue); 
     Debug.Log ("Correct"); 
     for(int i=0; i<=wValue.Length;i++){ 
      Debug.Log ("Enter"); 
      if(wValue[i]==gameObject.name[i]){ 
       Debug.Log ("Enter"); 
       gameObject.SetActive (false); 
      } 
     } 
    } 
} 
+2

['GameObject.Destroy(游戏对象)'](http://docs.unity3d.com/Documentation/ScriptReference/Object.Destroy.html)应做的伎俩。 –

+0

在我的游戏中,gameObject正在运行时创建名称,如A(克隆)GameObject.Destroy。(gameObject)不适用于它。我怎么能得到在运行时创建的gameObject – zahra

+0

我的意思是说,gameObject必须是对你想销毁的对象的引用。当你创建这个对象时,你应该在那个时候引用它,也许你可以将它们存储在一个列表中,然后通过并在以后删除它们?有点取决于如何布置所有东西 –

回答

0

如果你想删除的游戏对象,你可以使用Destroy (gameObject),或者虽然我不会建议它DestroyImmediate(gameObject)

否则,一个更有效的方法是使用gameObject.SetActive(false);

必要时停用,然后重新激活它,记得当我说游戏gameObject我的意思是一个游戏对象变量为竞赛对象消灭,例如:

using UnityEngine; 
using System.Collections; 

//You need to assign it in the Start() or from the editor 
public GameObject A; 

public class ActiveObjects : MonoBehaviour 
{ 
    void Start() 
    { 
     //to deactivate it 
     A.SetActive(false); 

     //to activate it 
     A.SetActive(true); 
    } 
} 

如果您要使用gameObject,您最终会删除/停用正在执行脚本的gameObject。

希望您觉得有用,

亚历