Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Update function can't take parameters?

Discussion in 'Scripting' started by momo5ms1, Jan 4, 2022.

  1. momo5ms1

    momo5ms1

    Joined:
    Feb 10, 2021
    Posts:
    20
    Hi everyone,
    I want to use Update() function with parameters but this seems not to be possible.
    is There any other way to use a function like update (called every frame) and pass parameters to it ?
    Best regards.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    How would adding parameters to update work? How would Unity know what to pass into those parameters?

    You can of course make your own function with any parameters you want and call it inside Update:
    Code (CSharp):
    1. void Update() {
    2.   MyCustomUpdate(Time.frameCount);
    3. }
    4.  
    5. void MyCustomUpdate(int frameNumber) {
    6.   print(frameNumber);
    7. }
     
    momo5ms1 likes this.
  3. momo5ms1

    momo5ms1

    Joined:
    Feb 10, 2021
    Posts:
    20
    Thank you for your reply, I got the idea and i haven't think about it, but i'm using an instance of another script, i'm passing it in parameters in my custom update function but when i call it in update i can't pass the parameters there.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Why can't you pass parameters? What is preventing you?
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    What is "arg"? Where is it coming from? What information is it supposed to hold?

    The error is complaining because this "arg" variable doesn't exist. You can't use variables that don't exist.
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    That's not where it's coming from, that's where it's going to.
     
    Deleted User likes this.
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Bunny83 likes this.
  8. momo5ms1

    momo5ms1

    Joined:
    Feb 10, 2021
    Posts:
    20
    I accept this as the best solution. Thank you