1
votes

I want to toggle between function with the left mouse click. So basically, I want my the screen to start with displaying func1(func1 is activated), then when you press the left mouse button func1 gets deactivated and func2 gets activated, and then when you press left mouse button again func2 gets deactivated and func3 gets activated and when you press the left mouse button again func3 gets deactivated and func1 get activated and this keeps looping.

bool tappedOnce = true;
    bool tappedTwice = false;
    bool tappedThrice = false;

    void func1() {print("func1");}
    void func2() {print ("func2");}
    void func3() {print ("func3");}
    void toggle()
    {
       if (Input.GetKeyDown(KeyCode.Mouse0)&& tappedOnce == false){tappedOnce = true; func1();}

       if (Input.GetKeyDown(KeyCode.Mouse0)&& tappedOnce && tappedTwice == false ){func2(); tappedTwice = true;}
       if (Input.GetKeyDown(KeyCode.Mouse0)&& tappedOnce && tappedTwice && tappedThrice == false){func3(); tappedOnce = false; tappedTwice = false;}

    }

Asking it here because this question was marked off topic on - game development stack exchange - and they suggested me to ask the question here.

1

1 Answers

0
votes

When switching between more than two different functionalities, I recommend using an integer and a switch case statement like this:

using UnityEngine;
using System.Collections;

public class MouseTest : MonoBehaviour {

//conditional
private int clickNumber;

//run code
void Update () 
{
    toggle();
}

//methods to run
void func0() {Debug.Log ("func0");}
void func1() {Debug.Log ("func1");}
void func2() {Debug.Log ("func2");}
void func3() {Debug.Log ("func3");}

//conditional code
void toggle()
{
    if(Input.GetMouseButtonDown(0))
        clickNumber += 1;

    switch (clickNumber)
    {
    case 0:
        func0();
        break;

    case 1:
        func1();
        break;

    case 2:
        func2();
        break;

    case 3:
        func3();
        break;

    default:
        break;
    }

    //set clickNumber to 0 when greater than the number of methods
    if(clickNumber > 3)
        clickNumber = 0;
}

}

Change the number of the last if statement to add more functions to the list.