为什么我的团结一致不起作用

时间:2016-05-23 16:12:54

标签: c# unity3d

我制作了2个脚本并将其分配给2个多维数据集:“cube”和“cube 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
        }
    }
}

有人可以帮帮我吗?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

尝试使用a.GetComponent<script>();。您需要将Type传递给GetComponent才能使其工作。我不确定你的代码是否编译,很难阅读,因为你没有正确地格式化它。