Search Unity

More function by OnMouseDown

Discussion in 'Scripting' started by Timoty75, Feb 15, 2019.

  1. Timoty75

    Timoty75

    Joined:
    May 29, 2017
    Posts:
    150
    Good morning guys I have a question: I interact with a character through the function OnMouseDown to make a certain event happen to the character and later, to train a tall of it, always in the same script, to make another event always happen to the same character. How can I do so that the first function is disabled after the first touch to the character to make to enter game the second function? Thank to all
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    You probably want to create a variable and use it to keep track of what's happened in the past, and make your function do different things depending on the value of the variable.

    For instance, you might declare
    int numberOfTimesClicked = 0;


    And then in OnMouseDown, you might write code something like:
    Code (CSharp):
    1. numberOfTimesClicked = numberOfTimesClicked + 1;
    2. if (numberOfTimesClicked == 1)  // this is the first click
    3. {
    4.     // Do whatever you do for the first click
    5. }
    6. else if (numberOfTimesClicked == 2)  // this is the second click
    7. {
    8.     // Do whatever you do for the second click
    9. }
     
  3. Timoty75

    Timoty75

    Joined:
    May 29, 2017
    Posts:
    150
    Don't work!
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    If you want help figuring out where you went wrong, you're going to need to provide a lot more information than that.

    Typically you should say:
    • What you wanted to have happen
    • A summary of your approach to the problem
    • What actually happened instead when you tried it out
    • The relevant portion of your source code (use code tags)