Function Which are outside of Button Method is not working When On Click Button ???

I have a button to fire and Fire() method which includes functions related to firing. I assign GameObjects in Awake Method so it wouldn’t have to assign them each time i click on button.

But button is not taking functions into consideration which are outside of Fire() method. Any suggestions ? Thanks…

What do you mean by “taking functions into consideration”? Your description of the situation is pretty vague. Maybe share some screenshots/descriptions of your setup, as well as whatever code is relevant, e.g. "Here’s the inspector for Object A with the OnClick handler registered to this object which… etc.

Hi @PraetorBlue Thanks for reply…

I mean by saying “taking functions into consideration” is that the functions which are exclusive of Fire() method is not being considered when i click on button. It only considers inside of Fire() method… I get errors saying “Object reference not set to an instance of an object.”
6205314--681426--fire213123.png

private void Awake()
    {
        GameObject myGun = GameObject.FindWithTag("Gun");

        MyGame myGameCs = GameObject.FindWithTag("MyGameTag").GetComponent<MyGame>();

        Animator myAnimator = GameObject.FindWithTag("Player").GetComponent<Animator>();
    }

public void Fire()
    {
        if (!myGameCs.playerDied && myGameCs.ammo > 0)
        {
            myGameCs.ammo -= 1;

            myAnimator.SetTrigger("isFire");

            fireEffectInstance = (GameObject)Instantiate(fireEffect, myGun.transform.position, fireEffect.transform.rotation);
        }
    }

What is your exact error message, including the line numbers part at the end?

if i assign objects in fire method, they will be asssigned everytime i press on button. This will cause delays and problems.

Are the “Gun”, “Player”, and “MyGameTag” objects in the scene from the beginning? Or do they get spawned/instantiated into the scene by some script?

Fire() method can not take reference from Awake method. This is the main problem. I’am getting “NullReferenceException” error.

Yes I understand that. Can you please paste the full error message including the numbers at the end as it contains more information such as the exact line number on which it occurs.

1 Like

yes they are already in scene and there’s not problem to find them. They are being assigned.

I can share but there’s no problem by assigning them. Errors occurs when i reference them from Awake() to Fire() method. Button wants all functions to be inside Fire() or it wouldn’t find them…

In this screenshot:

Is the “BlueW…” object referencing a prefab or is it referencing an object in the scene?

Clearly it is having trouble finding them, or you wouldn’t have this error.

@PraetorBlue You can try it by a simple script. The codes which are outside of Button Method is not being included.

it’s a prefab…which is being instantiated as a new gameobject on start. Then when i fire, it can’t find the assigned gameobjects…

That’s your problem. You’ll want to have your button reference an actual object in the scene, not a prefab. I’m not even sure Awake gets called on the prefab itself, and if it does, it’s probably before your scene is even loaded.

I instantiate prefab as a new gameobject. I always use this method and the clone object works well in other cases. I will try to reference an alive object and see what happens. Thanks for helping…:slight_smile:

fireEffectInstance = (GameObject)Instantiate(fireEffect, myGun.transform.position, fireEffect.transform.rotation);

it’s not the fire effect I’m talking about. I’m talking about the screenshot where your button OnClick listener is referencing the PlayerMovement object. Is that PlayerMovement object in the scene or is it a prefab?

BlueW is prefab object and PlayerMovement is the script to take Fire() method from prefab.

Yes exactly. The fact that your button OnClick listener is referencing a prefab is what is causing your issue.

1 Like

Yes i think so too. Thanks for helping…<3