2016-05-23 78 views
1

我製作了2個腳本,並將其分配給2個立方體:「立方體」和「立方體1」。 通常,如果您單擊多維數據集,它會設置一個值,以便當您單擊多維數據集1時它會消失。 如果先點擊立方體1,它不會工作。 這就是我試圖做的,但它不起作用,我不明白爲什麼。爲什麼我的腳本統一不起作用

這裏是我的腳本

立方體:

using UnityEngine; 
using System.Collections; 

public class script : MonoBehaviour 
{ 

    public int test = 0; // make the variable for the value 
    public void OnMouseDown() // when the user click 
    { 
     test = 1; //make the value of test 1 
    } 
} 

立方體1

using UnityEngine; 
using System.Collections; 

public class NewBehaviourScript1 : MonoBehaviour 
{ 
    public GameObject a; //make a gameobject 
    public script script; //make a variable where we put in the script 
    void OnMouseDown() // when the user click 
    { 
     script = a.GetComponent<script>(); // get script 

     if (script.test == 1) //test or the variable test in the other script is 1 
     { 
     Destroy(gameObject); // destroy the object 
     } 
    } 
} 

有人可以幫我嗎?

+0

你可以添加現在正在發生的事情嗎?編輯:我對統一很陌生,但也許,如果你檢查[GetComponent()](http://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html)的文檔,它會返回附加到您正在調用該函數的對象。所以...可能會做一些像cube.GetCompoment()? Idk,只是我的猜測。 – Meraj99

回答

0

嘗試使用a.GetComponent<script>();。您需要將Type傳遞給GetComponent才能使其工作。我不確定你的代碼是否編譯完成,很難閱讀它,因爲你沒有正確格式化它。

1

script類的名稱更改爲大寫。

public class Script : MonoBehaviour 

然後在NewBehaviourScript1變化裏面的一切它:

public class NewBehaviourScript1 : MonoBehaviour 
{ 
    public Script script; //Drag the other cube onto this in the inspector. 

    void OnMouseDown() 
    { 
     if (script.test == 1) 
     { 
      Destroy(gameObject); 
     } 
    } 
} 

你應該用你的類和它們的實例都更具描述性的名稱。

注意:爲此,您將必須將其他立方體拖動到檢查器中的script變量上,並且它必須連接一個Script

+0

它stils什麼都不做 –

相關問題