2016-05-23 60 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什么都不做 –

相关问题