C# 从OnTiggerEnter类获取变量到另一个类

C# 从OnTiggerEnter类获取变量到另一个类,c#,unity3d,C#,Unity3d,我有一个非常简单的问题,我无法理解我遗漏了什么。 我得到一个错误: NullReferenceException:对象引用未设置为对象的实例 Update()(位于Assets/Scripts/test.cs:13) 所以,我有一个类“DissableInput”,其中有一个简单的触发器,当它被另一个对象触发时,它就会改变。无论何时更改,它都返回true/false 另一个类被命名为“test”,它基本上应该根据触发器输入在为真/假时打印出来 //测试班 public bool touchedC

我有一个非常简单的问题,我无法理解我遗漏了什么。 我得到一个错误:

NullReferenceException:对象引用未设置为对象的实例 Update()(位于Assets/Scripts/test.cs:13)

所以,我有一个类“DissableInput”,其中有一个简单的触发器,当它被另一个对象触发时,它就会改变。无论何时更改,它都返回true/false

另一个类被命名为“test”,它基本上应该根据触发器输入在为真/假时打印出来

//测试班

public bool touchedCollision;

// Update is called once per frame
void Update()
{
    if (this.GetComponent<DissableInput>().touchedCollision)
    {
        Debug.Log("IN COLL");
    }
    if(!this.GetComponent<DissableInput>().touchedCollision)
    {
        Debug.Log("NOT IN COLL");
    }
}
public bool touchedCollision; //you already had this, but weren't using it

// Update is called once per frame
void Update()
{
    if (touchedCollision)
    {
        Debug.Log("IN COLL");
    }
    if(!touchedCollision) //can be changed to just `else`
    {
        Debug.Log("NOT IN COLL");
    }
}

我希望true/false会进入测试类,但它会给出NullReferenceException错误。

这部分
这个.GetComponent().touchedCollision
将尝试在其连接的同一游戏对象中获取指定类型的组件,在本例中是“DisableInput”类。如果您希望在另一个游戏对象中使用DisableInputClass脚本,则需要以其他方式进行引用

使用公共变量看起来是这样的

//测试类

public DisableInput disableInput;

// Update is called once per frame
void Update()
{
    if (disableInput.touchedCollision)
    {
        Debug.Log("IN COLL");
    }
    else //you don`t need to specify the condition again, you can do it with and else
    {
        Debug.Log("NOT IN COLL");
    }
}
  private DisableInput disableInput;

void Awake(){
  disableInput = FindObjectOfType<DisableInput>(); 
// you only need to get the 
//class refence one time, no need for it to be in the update, 
//it gains a lot of performance
}

    // Update is called once per frame
    void Update()
    {
        if (disableInput.touchedCollision)
        {
            Debug.Log("IN COLL");
        }
        else //you don`t need to specify the condition again, you can do it with and else
        {
            Debug.Log("NOT IN COLL");
        }
    }
您可以查看GetComponent是如何工作的

另一种方法是使用
FindObjectOfType()
//测试类

public DisableInput disableInput;

// Update is called once per frame
void Update()
{
    if (disableInput.touchedCollision)
    {
        Debug.Log("IN COLL");
    }
    else //you don`t need to specify the condition again, you can do it with and else
    {
        Debug.Log("NOT IN COLL");
    }
}
  private DisableInput disableInput;

void Awake(){
  disableInput = FindObjectOfType<DisableInput>(); 
// you only need to get the 
//class refence one time, no need for it to be in the update, 
//it gains a lot of performance
}

    // Update is called once per frame
    void Update()
    {
        if (disableInput.touchedCollision)
        {
            Debug.Log("IN COLL");
        }
        else //you don`t need to specify the condition again, you can do it with and else
        {
            Debug.Log("NOT IN COLL");
        }
    }
私有禁用输入禁用输入;
无效唤醒(){
disableInput=FindObjectOfType();
//你只需要拿到
//类引用一次,不需要在更新中,
//它获得了很多性能
}
//每帧调用一次更新
无效更新()
{
if(禁用输入.触摸碰撞)
{
Debug.Log(“在COLL中”);
}
else//您不需要再次指定条件,可以使用和else进行指定
{
Log(“不在COLL中”);
}
}
关于FindObjectOfType的更多信息,但要使用它,您必须了解
它将返回他找到的第一个附加了DisableInput的对象。如果您有多个具有DisableInput的游戏对象,您无法确定它将获得哪一个。

此部分
此.GetComponent().touchedCollision
将尝试在与其连接的同一游戏对象中获取指定类型的组件,在本例中为“DisableInput”类。如果您希望在另一个游戏对象中使用DisableInputClass脚本,则需要以其他方式进行引用

使用公共变量看起来是这样的

//测试类

public DisableInput disableInput;

// Update is called once per frame
void Update()
{
    if (disableInput.touchedCollision)
    {
        Debug.Log("IN COLL");
    }
    else //you don`t need to specify the condition again, you can do it with and else
    {
        Debug.Log("NOT IN COLL");
    }
}
  private DisableInput disableInput;

void Awake(){
  disableInput = FindObjectOfType<DisableInput>(); 
// you only need to get the 
//class refence one time, no need for it to be in the update, 
//it gains a lot of performance
}

    // Update is called once per frame
    void Update()
    {
        if (disableInput.touchedCollision)
        {
            Debug.Log("IN COLL");
        }
        else //you don`t need to specify the condition again, you can do it with and else
        {
            Debug.Log("NOT IN COLL");
        }
    }
您可以查看GetComponent是如何工作的

另一种方法是使用
FindObjectOfType()
//测试类

public DisableInput disableInput;

// Update is called once per frame
void Update()
{
    if (disableInput.touchedCollision)
    {
        Debug.Log("IN COLL");
    }
    else //you don`t need to specify the condition again, you can do it with and else
    {
        Debug.Log("NOT IN COLL");
    }
}
  private DisableInput disableInput;

void Awake(){
  disableInput = FindObjectOfType<DisableInput>(); 
// you only need to get the 
//class refence one time, no need for it to be in the update, 
//it gains a lot of performance
}

    // Update is called once per frame
    void Update()
    {
        if (disableInput.touchedCollision)
        {
            Debug.Log("IN COLL");
        }
        else //you don`t need to specify the condition again, you can do it with and else
        {
            Debug.Log("NOT IN COLL");
        }
    }
私有禁用输入禁用输入;
无效唤醒(){
disableInput=FindObjectOfType();
//你只需要拿到
//类引用一次,不需要在更新中,
//它获得了很多性能
}
//每帧调用一次更新
无效更新()
{
if(禁用输入.触摸碰撞)
{
Debug.Log(“在COLL中”);
}
else//您不需要再次指定条件,可以使用和else进行指定
{
Log(“不在COLL中”);
}
}
关于FindObjectOfType的更多信息,但要使用它,您必须了解 它将返回他找到的第一个附加了DisableInput的对象。如果您有多个具有DisableInput的游戏对象,您无法确定它将获得哪一个。

您的脚本位于不同的对象上,因此
此.GetComponent
将无法工作 您想知道ObjA何时接触ObjB,因此在ObjA上有一个脚本想知道,在ObjB上有一个脚本在两个对象接触时运行。但是,
this.GetComponent
只能看到附加到同一对象的脚本。所以您不能从ObjA运行此代码(因为它不知道,也不能知道ObjB!)

我将对您的脚本进行两个小更改:

//测试班

public bool touchedCollision;

// Update is called once per frame
void Update()
{
    if (this.GetComponent<DissableInput>().touchedCollision)
    {
        Debug.Log("IN COLL");
    }
    if(!this.GetComponent<DissableInput>().touchedCollision)
    {
        Debug.Log("NOT IN COLL");
    }
}
public bool touchedCollision; //you already had this, but weren't using it

// Update is called once per frame
void Update()
{
    if (touchedCollision)
    {
        Debug.Log("IN COLL");
    }
    if(!touchedCollision) //can be changed to just `else`
    {
        Debug.Log("NOT IN COLL");
    }
}
//禁用输入类

public bool touchedCollision;

void OnTriggerEnter (Collider other)
{
    Debug.Log("In Triger");
    touchedCollision = true;
}

public void OnTriggerExit(Collider other)
{
    Debug.Log("Out Triger");
    touchedCollision = false;
}
//removed touchedCollision from here

void OnTriggerEnter (Collider other)
{
    Debug.Log("In Triger");
    other.GetComponent<TestClass>().touchedCollision = true;
}

public void OnTriggerExit(Collider other)
{
    Debug.Log("Out Triger");
    other.GetComponent<TestClass>().touchedCollision = false;
}
使用。

脚本位于不同的对象上,因此
此.GetComponent
将无法工作 您想知道ObjA何时接触ObjB,因此在ObjA上有一个脚本想知道,在ObjB上有一个脚本在两个对象接触时运行。但是,
this.GetComponent
只能看到附加到同一对象的脚本。所以您不能从ObjA运行此代码(因为它不知道,也不能知道ObjB!)

我将对您的脚本进行两个小更改:

//测试班

public bool touchedCollision;

// Update is called once per frame
void Update()
{
    if (this.GetComponent<DissableInput>().touchedCollision)
    {
        Debug.Log("IN COLL");
    }
    if(!this.GetComponent<DissableInput>().touchedCollision)
    {
        Debug.Log("NOT IN COLL");
    }
}
public bool touchedCollision; //you already had this, but weren't using it

// Update is called once per frame
void Update()
{
    if (touchedCollision)
    {
        Debug.Log("IN COLL");
    }
    if(!touchedCollision) //can be changed to just `else`
    {
        Debug.Log("NOT IN COLL");
    }
}
//禁用输入类

public bool touchedCollision;

void OnTriggerEnter (Collider other)
{
    Debug.Log("In Triger");
    touchedCollision = true;
}

public void OnTriggerExit(Collider other)
{
    Debug.Log("Out Triger");
    touchedCollision = false;
}
//removed touchedCollision from here

void OnTriggerEnter (Collider other)
{
    Debug.Log("In Triger");
    other.GetComponent<TestClass>().touchedCollision = true;
}

public void OnTriggerExit(Collider other)
{
    Debug.Log("Out Triger");
    other.GetComponent<TestClass>().touchedCollision = false;
}

使用。

您听说过静态变量吗?这些脚本是否附加到不同的对象?尼古拉:静态不是正确的解决方案。静态没有帮助,是的,这两个不同的脚本附加到两个不同的对象上。@Draco18s我不知道它怎么不正确,下面的答案正是静态的作用。但是嘿!“我也想看看下面你的答案。”尼古拉。始终假定对象是以非单例方式实例化的,除非有相反的证据。因此,
static
关键字仅在值实际上是正确的静态值时使用。一般经验法则:这个用例没有通过测试。你听说过静态变量吗?这些脚本是附加到不同的对象上的吗?尼古拉:静态不是正确的解决方案。静态没有帮助,是的,这两个不同的脚本附加到两个不同的对象上。@Draco18s我不知道它怎么不正确,下面的答案正是静态的作用。但是嘿!“我也想看看下面你的答案。”尼古拉。始终假定对象是以非单例方式实例化的,除非有相反的证据。因此,
static
关键字仅在值实际上是正确的静态值时使用。一般经验法则:这个用例没有通过测试。Felipe,第一个例子给出了相同的结果:nullreferenceException,但是秒运行得很好。谢谢你给我一个选择!您需要在inspector中引用它,它才能工作。很高兴有帮助,如果你不介意的话,把答案定为有用的。我不知道。