Search code examples
c#unity-game-engine2d-games

Unity - How can i make a function fire only when player clicked on it 3 times?


I have a hint button on my game, but it fires up everytime you click on it. I need for it to fire only once every 3 clicks

i've tried adding a loadCount = 0 and an if statement

if (loadCount % 3 == 0) { }

but it dosen't seem to work

image for reference : https://ibb.co/L9LnNDw

Here is the script:

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

[ExecuteInEditMode]
public class HintScript : MonoBehaviour 
{

    LineRenderer line;

    // Use this for initialization
    void Start () 
    {

    }

    // Update is called once per frame
    void Update () 
    { 
        line = GetComponent<LineRenderer>();
        line.positionCount = transform.childCount;
        for (int i = 0; i<transform.childCount; i++)
        {
            line.SetPosition(i, transform.GetChild(i).position);
        }
    }

    // This is called from onClick of the Button
    public void Hint() 
    { 
        FindObjectOfType<AdMobManager>().Hint = true; 
        FindObjectOfType<AdMobManager>().showInterstitial(); 
    }
}

Solution

  • You should use a simply counter.

    I also changed and commented other "issues" in your code:

    public class HintScript : MonoBehaviour 
    {
        // by adding SerializeField you can already set those references
        // directly in the Unity Editor. This is better than getting them at runtime.
        [SerializeField] private LineRenderer line;
        [SerializeField] private AdMobManager adMobManager;
        // you can still change the required clicks in the inspector
        // Note: afterwards changing it here will have no effect!
        [SerializeField] private int requiredClicks = 3;    
    
        // counter for your clicks
        private int counter;
    
        // Use this for initialization
        void Start () 
        {
            // you should do this only once
            line = GetComponent<LineRenderer>();
    
            // you should also this only do once
            adMobManager = FindObjectOfType<AdMobManager>();
    
            // If instead you would drag those references directly into the now
            // serialized fields you wouldn't have to get them on runtime at all.
        }
    
        // Update is called once per frame
        void Update () 
        { 
            line.positionCount = transform.childCount;
            var positions = new Vector3[transform.childCount];
            for (var i = 0; i < transform.childCount; i++)
            {
                position[i] = transform.GetChild(i).position;
            }
    
            // it is more efficient to call this only once
            // see https://docs.unity3d.com/ScriptReference/LineRenderer.SetPositions.html
            line.SetPositions(positions);
        }
    
        // This is called from onClick of the Button
        public void Hint() 
        { 
            counter++;
    
            if(counter < requiredClicks) return;
    
            // Or using your solution should actually also work
            if(counter % requiredClicks != 0) return;
    
            adMobManager.Hint = true; 
            adMobManager.showInterstitial(); 
    
            // reset the counter
            counter = 0;
        }
    }