Shadow Lev
Shadow Lev

Reputation: 3

When I try to use a gameObject, thats outside the "OnCollisionEnter" function it does not work

When I try to use a gameObject, thats outside the "OnCollisionEnter" function it says "Object reference not set to an instance of an object"

private void OnCollisionEnter(Collision other) 
{
    if(other.gameObject.tag == "Finish")
    {
        //temporary object (created outside the function)
        tempGO = other.gameObject;
        Instantiate(particles, transform.position, Quaternion.identity);

        if(GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Gun>().fn90)
        {
            tempGO.GetComponent<Health>().Dmg(3);
        }
        else if(!GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Gun>().fn90)
        {
            tempGO.GetComponent<Health>().Dmg(10);
        }

        tempGO = null;
        Destroy(gameObject);
    }
}

There is an error on 20th line (first if, the script couldnt find the "other.gameObject")

Upvotes: 0

Views: 23

Answers (0)

Related Questions