2016-05-23 7 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>();を試してみてください。 GetComponentを動作させるには、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); 
     } 
    } 
} 

あなたのクラスとそのインスタンスの両方のために、より説明的な名前を使用する必要があります。

注:これが機能するには、は、もう1つのキューブをインスペクタのscript変数にドラッグする必要があります。Scriptが添付されている必要があります。

+0

それは何もしません –

関連する問題