Alternatives to Update() that takes parameters

Hello,
I’m making a 2D platform shooter(my first game)
I have watched a tutorial to implement shooting. The shooting works good but I have programmed my gun to be hidden/disabled when jumping and crouching. The problem now is that there are still bullets spawning when the gun is hidden. I tought an easy solluction would be to implement a public bool to only shoot when the gun is shown but Update() functions apperently do not take parameters.

This is the original code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Weapon : MonoBehaviour
{
    public Transform firePoint;
    public GameObject bulletPrefab1;

    public void Update()
    {
        if (Input.GetButtonDown("Fire1") )
        {
            Shoot();
        }
    }

    void Shoot()
    {
        //shooting logic
        Instantiate(bulletPrefab1, firePoint.position, firePoint.rotation);
    }
}

This is the sollution i tought would work(but doesn’t):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Weapon : MonoBehaviour
{
    public Transform firePoint;
    public GameObject bulletPrefab1;

    public void Update(bool ShowGun)
    {
        if (Input.GetButtonDown("Fire1") && ShowGun == true)
        {
            Shoot();
        }
    }

    void Shoot()
    {
        //shooting logic
        Instantiate(bulletPrefab1, firePoint.position, firePoint.rotation);
    }
}

Does anyone have a idea how to make this work properly?

Where do you think these parameters would come from? When Unity calls your Update, how is it supposed to have any idea what ShowGun’s value is supposed to be?

You can make a “public bool ShowGun” next to your public Transform & GameObject members. This can be checked/unchecked in the inspector, and could also be affected by other scripts. So the way you would change this value depends on where ShowGun’s value comes from.

I have another script that tells the other scripts the value of ShowGun, I know that method works because I also use it to show and hide my gun. But it doesn’t work with the Update() function.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{

    public CharacterController2D controller;

    public float runSpeed = 40f;
    public Animator animator;

    public GameObject my_gun;
    public GameObject my_bullet;
    Weapon my_bullet_script;
    gunshow my_gun_script;
    bool ShowGun;


    float horizontalMove = 0f;
    bool jump = false;
    bool crouch = false;

  
    void Start()
    {
        my_gun_script = my_gun.GetComponent<gunshow>();
        my_bullet_script = my_bullet.GetComponent<Weapon>();
        ShowGun = true;
        my_bullet_script.Update(ShowGun);

    }
    void Update()
    {
        horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;

        animator.SetFloat("Speed", Mathf.Abs(horizontalMove));
        my_gun_script.GunRunAnimation(horizontalMove);

        if (Input.GetButtonDown("Jump"))
        {
            jump = true;
            animator.SetBool("IsJumping", true);
            ShowGun = false;
            my_gun_script.guns(ShowGun); //ShowGun bool = false, so gun shouldn't be showing
            my_bullet_script.Updtae(ShowGun);
        }

        if (Input.GetButtonDown("Crouch"))
        {
            crouch = true;
        } else if (Input.GetButtonUp("Crouch"))
        {
            crouch = false;
        }
       
    }

    public void OnLanding()
    {
        animator.SetBool("IsJumping", false);
        ShowGun = true;
        my_gun_script.guns(ShowGun); //ShowGun bool = true, so gun should be showing
        my_bullet_script.Update(ShowGun);
    }

    public void OnCrouching (bool isCrouching)
    {
        animator.SetBool("IsCrouching", isCrouching);
        if (isCrouching == true)
        {
            ShowGun = false;
            my_gun_script.guns(ShowGun);
            my_bullet_script.Update(ShowGun);
        }
        else if (isCrouching == false)
        {
            ShowGun = true;
            my_gun_script.guns(ShowGun);
            my_bullet_script.Update(ShowGun);
        }

    }

    void FixedUpdate()
    {
        //move character
        controller.Move(horizontalMove * Time.fixedDeltaTime, crouch, jump);
        jump = false;
       
    }
}

Can you tell me how I can change the public bool with other scripts if I put it outside of the function like you said?

In Weapon:

    public Transform firePoint;
    public GameObject bulletPrefab1;
public bool ShowGun = true;

In PlayerMovement:

if (Input.GetButtonDown("Jump"))
{
...
my_bullet_script.ShowGun = true;
}

Note that you shouldn’t call Update from PlayerMovement; let the engine handle it for you. Update() is called by the Unity engine each frame, and adding overloads of Update and calling them yourself will only add confusion. In your posted code, Weapon’s Update(bool) would be called on line 46, but it’d only be called that way one frame; and because it has a parameter, Unity won’t recognize it as “the Update function” of the script, and won’t call it each frame like it calls Update().

It is possible to set up a system where you have a single “manager” class calling updates (which are explicitly not Update(), but some other update function you create) on other objects, which in certain scenarios has some advantages. The two biggest advantages are:

  1. You can execute these function in a precise order that you define. If A must happen before B and C, and D only sometimes happens, you can write that all into the script without futzing around with the script execution order panel.
  2. Speed, when dealing with large numbers of objects. There’s a little bit of overhead when Unity call Update, because it’s doing so from C++ code, and crossing that bridge isn’t free. If you have thousands of objects, looping through them from a single Update() on a manager class can be faster than each of them having their own Update(). (Something similar to this principle is one of the many sources of speed improvements in the new ECS system.)
    …and yes, if you set up such a system, you could add parameters to your update function if you like. I generally haven’t found that to be beneficial, however.

Thank you very much for your explanation!
It works now, sometimes it’s easier than i thought.