简体   繁体   English

Unity3D:使用OnTriggerEnter2D的脚本变量不一致

[英]Unity3D: Script variables inconsistences using OnTriggerEnter2D

I have the object that have the script component. 我有具有脚本组件的对象。

public class TeleportReference : MonoBehaviour {
    private GameObject reference;

    void Start () {
        reference = null;
    }

    public void SetReference(GameObject other){
        reference = other;
    }

    public GameObject GetReference(){
        return reference;
    }

Now if I search for an object and test the script variables 现在,如果我搜索对象并测试脚本变量

GameObject test = GameObject.FindGameObjectWithTag("Water");
print(test);
print(test.GetComponent<TeleportReference>());
print(test.GetComponent<TeleportReference>().GetReference());

it works just fine and GetReference() return the variable I stored. 它工作正常,并且GetReference()返回我存储的变量。

But now if I use it whithin OnTriggerEnter2D 但是现在如果我在OnTriggerEnter2D中使用它

private void OnTriggerEnter2D(Collider2D other)
{
    if (other.tag == "Water")
    {
        print(other);
        print(other.gameObject);
        print(other.GetComponent<TeleportReference>());
        print(other.GetComponent<TeleportReference>().GetReference());
    }
}

GetReference() return null (or other variable that I used during Start() of TeleportReference class). GetReference()返回null(或在TeleportReference类的Start()中使用的其他变量)。 All other testing outputs remain the same. 所有其他测试输出保持不变。

Could anyone give a hint why this could happen? 有人可以暗示为什么会发生这种情况吗? Does that mean that GetComponent created new instance of TeleportReference in second case? 这是否意味着在第二种情况下,GetComponent创建了TeleportReference的新实例?

GetComponent did not create a new instance of TeleportReference in second case. 在第二种情况下,GetComponent没有创建TeleportReference的新实例。 I have similar code in one of my projects and I haven't had any problems. 我在一个项目中有类似的代码,但没有任何问题。 So in this case I would look to see if the problem is somewhere else. 因此,在这种情况下,我将查看问题是否在其他地方。 Are you sure it's the same "Water" object? 您确定它是相同的“水”对象吗? Do you have multiple objects with "Water" tag? 您是否有多个带有“水”标签的对象? Are you colliding before reference is assigned? 您是否在分配参考之前发生冲突? There could be a myriad of things going on. 可能发生了无数的事情。 Just test to narrow it down. 只需测试以缩小范围即可。

For example try performing an action on the other.gameObject object to verify it's the right object, like deactivating it. 例如,尝试对other.gameObject对象执行操作以确认它是正确的对象,例如将其禁用。 Also try using other.gameObject.GetComponent (not sure if this makes a difference). 也可以尝试使用other.gameObject.GetComponent(不确定是否有所不同)。

Found the issue. 找到了问题。 It was in the order of assigning variables. 这是按照分配变量的顺序进行的。 That is - changes were made apperantly before Start() function was called. 也就是说,在调用Start()函数之前已进行了相应的更改。 Thus at some point of the code the stored value was overritten back to null. 因此,在代码的某个点上,存储的值被改写回null。

This works fine: 这工作正常:

public class TeleportReference : MonoBehaviour {
    private GameObject reference;

    void Awake () {
        reference = null;
    }

    public void SetReference(GameObject other){
        reference = other;
    }

    public GameObject GetReference(){
        return reference;
    }

Thanks everyone for comments, that really helped me in debugging. 感谢大家的评论,这确实对我的调试有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM