How to make Boolean not add more than one integer?

So I’m implementing ads into the game and the problem is that I have it set that when the character dies a certain amount of times, the ads shows up.

Heres the code so far:

using UnityEngine;
using System.Collections;
using UnityEngine.Advertisements;

public class AdsScript : MonoBehaviour {
	public float addsing;
	void Start()
	{
		addsing = 0f;
		DontDestroyOnLoad (this.gameObject);


	}

	void Update()
	{
		GameObject thePlayer = GameObject.Find ("fly");
		TouchScript touchScript = thePlayer.GetComponent<TouchScript> ();

		if (touchScript.dead)
			addsing += 1f;
		return;

		if (touchScript.dead && addsing == 3) {
			if (Advertisement.IsReady ()) {
				Advertisement.Show ();
				addsing = 0;
			}
		}

	}


}

The problem is that whenever the boolean dead becomes true, the script doesn’t just add one integer but keeps on adding.

Is there a way to make it only add one integer when dead becomes true?

In its current state, your script won’t do what you intended. return; on line 22 is called every time, so nothing after that line will ever be reached, and all your script does is incrementing addsing while touchScript.dead is true.

This script could be fixed, but a more elegant solution would be to use events. That way, you don’t have to check at every update if the player’s dead, you just define a method that gets called whenever the player dies, which is what you wanted.

First, in TouchScript, create a UnityEvent and invoke it whenever the player dies.

using UnityEngine.Events;

public class TouchScript 
{
        ...
        // Define a new UnityEvent
        public UnityEvent onDead = new UnityEvent();

        void Die() 
        {
                ...
                // Invoke it when the player dies
                onDead.Invoke()
        }
}

Then, in your AdsScript, you have to listen to this event and add a callback.

void Start()
{
         addsing = 0f;
         DontDestroyOnLoad (this.gameObject);
         
         // Use GameObject.Find only once
         GameObject thePlayer = GameObject.Find ("fly");
         TouchScript touchScript = thePlayer.GetComponent<TouchScript> (); 

         // Add listener to the event you just defined
         touchScript.onDead.AddListener(onPlayerDead);
}

void onPlayerDead()
{
        // Called whenever the player dies
        addsing += 1f;
        if (addsing == 3) 
        {
             if (Advertisement.IsReady ()) {
             Advertisement.Show ();
             addsing = 0;
         }
}

Never use GetComponent functions inside Update function unless it is really, really necessary.