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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Passing variable value from public function to update() function

Discussion in 'Scripting' started by rcserv2617, Dec 30, 2017.

  1. rcserv2617

    rcserv2617

    Joined:
    Sep 28, 2017
    Posts:
    5
    Hi All,

    I'm kind of new to Unity and with some little experience in c#. I'm currently working on a project where I have a function which is being called when user presses a button (called public void OnMouseDown). It then changes the value of an int clicked and passes the value to void update().

    Basically the flow is as follows:

    1. When user presses the button, the function OnMouseDown is called
    2. This changes the value of "clicked" to 1 (initialised as 0)
    3. The value of "clicked" needs to be passed to Update()
    4. An action such as movement of Sprite is performed in Update()

    My challenge is that I am not able to pass the updated value of "clicked" to Update(). Is there a best practise to do this in c#? Any help will be much appreciated.

    Thank you!
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, a few things..
    1) I wouldn't name the method "OnMouseDown" as that is a reserved method name in Unity. If it's for a UI button, I'd change the name. If you're clicking an object, and actually meant to use OnMouseDown, just ignore that.
    2) You can simply check if clicked is '1' in Update, if you want (and need to use it in Update()).
    Something like:
    Code (csharp):
    1.  
    2. void Update() {
    3.   if(clicked == 1) {
    4.      // do something.
    5.      // possibly set clicked to '0' , not sure.
    6.     }
    7.  }
    3) if you do not need it to run in Update(), then you should just do the code in the method that is called when you click the button (or call some other method from there).
     
  3. rcserv2617

    rcserv2617

    Joined:
    Sep 28, 2017
    Posts:
    5
    Thanks. I changed the name of the method to On_Button_Clicked().

    The method On_Button_Clicked is called when a UI Button is pressed. And within this method I am assigning clicked to be 1.

    But when I check the value of clicked in Update() it reverts back to 0 (which is set at Start()). Any idea what is happening here? Perhaps the updated value of clicked is not passed to Update()?

    Any advise is greatly appreciated.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You script has to look something like this:
    Code (csharp):
    1.  
    2. int clicked = 0;
    3.  
    4. public void On_Button_Clicked() {
    5.     clicked = 1;
    6.     // do not do the following:
    7.     //    int clicked = 1;
    8.    }
    9. void Update() {
    10.    if(clicked == 1) {
    11.       // do some stuff
    12.      }
    13.  }
    14.  
    If it's being set to 1, then back to 0 somehow, just check your code to see if you did that elsewhere. Otherwise, it should be working.
     
  5. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    It sounds like it would just be easier to start a co-routine when you click the button or if you are just moving a sprite, use the animator.
     
  6. MickM

    MickM

    Joined:
    Nov 19, 2012
    Posts:
    166
    As methos5k has said - the best practice is to call a method on your object which sets the variable. Update then just acts based on the current values of variables as required (eg. Move the object if the move flag is set to true).


    If you are using the inbuilt OnMouseDown function I would recommend you consider an alternative approach. This video is a tutorial on selection boxes but discusses the disadvantages of those functions in the first ~6 minutes (Probably worth watching the full video though):



    The mouse manager raycast method that is used is a better way in my opinion. In this implementation you can use the getComponent method and set whatever flag you need; something like

    Code (CSharp):
    1.  
    2. TYPE theScript = hitInfo.collider.gameObject.getComponent<TYPE>();
    3. if (theScript != null)
    4. {
    5.     theScript.SetMoveFlag(1);
    6. }
    Then as per methos5k's reply, the update function takes action based off the value you have set.

    edit: Just saw the replies written while I was typing this; if it is done via UI button then methos5k's solution is the way to go. I had assumed you were using the built in Monobehaviour OnMouse family of functions.
     
  7. rcserv2617

    rcserv2617

    Joined:
    Sep 28, 2017
    Posts:
    5
    methos5k:

    Did exactly as you mentioned and put in a few print statements to see what was happening as well.

    1. When the UI button is clicked, the function On_Button_Clicked is called and the value of "clicked" becomes 1
    2. But once it goes into Update() the value of "clicked" becomes 0
    3. Tried to set the initialisation value of "clicked" to "2" by setting: int clicked = 2;
    4. When I do step 2, then the value returned by "clicked" in becomes 2.

    Question: Does the value of "clicked" get initialised before calling each Update() function? Sorry for so many questions, I'm trying to figure this our logically.
     
  8. rcserv2617

    rcserv2617

    Joined:
    Sep 28, 2017
    Posts:
    5
    Thank you. I just replied to methos5k as well. Seems like the variable gets initialised every time it goes into Update()
     
  9. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    No, the value would not reset. Do you have multiple copies of this script in your scene?
     
  10. MickM

    MickM

    Joined:
    Nov 19, 2012
    Posts:
    166
    To save confusion, it might be best if you just post your code.
     
    TaleOf4Gamers likes this.
  11. rcserv2617

    rcserv2617

    Joined:
    Sep 28, 2017
    Posts:
    5
    Hi all,

    The code works now. I just detached the script from my UI button and re attached it again and it seems to work now. Thank you for the help. Appreciate it!
     
  12. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ah, well good stuff :) Glad you got it working.
     
    rcserv2617 likes this.