0

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.

But now if I use it whithin 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). 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?

Alex
  • 189
  • 1
  • 1
  • 10
  • Where's your OnTriggerEnter2D code? it's attached to the object with the tag "Water"? it's attached to a different object than "Water"? – Roberto Guajardo Jan 26 '16 at 13:28
  • When colliders enter each other (and if they're both triggers), `OnTriggerEnter2D` is called on each of them. I suggest you `Debug.Log(other.gameObject.name);` to ensure that only the intended objects have this code called on them. I hope that helps! – andeart Jan 26 '16 at 18:05

2 Answers2

1

GetComponent did not create a new instance of TeleportReference in second case. 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. Also try using other.gameObject.GetComponent (not sure if this makes a difference).

Paul Cristea
  • 453
  • 4
  • 10
  • Thanks Paul, I've tested it hard. In this case there is only one "Water" object. And the first test that worked fine was made before the collision. I struggle as I don't see what could go wrong here. – Alex Jan 26 '16 at 10:21
0

Found the issue. It was in the order of assigning variables. That is - changes were made apperantly before Start() function was called. Thus at some point of the code the stored value was overritten back to 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.

Alex
  • 189
  • 1
  • 1
  • 10