Boolean issue: Flag mechanics

Hello, recently I have hit a roadblock with flag mechanics with my “capture the flag” style game.

My issue

Everything works just fine until the player returns the flag to it’s base. When this happens, I need to stop a coroutine in my modular flag script. I use a function to communicate between my scripts. The said function has a boolean parameter. When the function is called, it passes the boolean into the other script, updates another boolean, sets it to false, which is correct, but then resets to true!?

I know that was a lot of information, so lets take a look at the scripts now, shall we?

The scripts
//This is my player script. I might not need to involve two scripts in my code but it made sense at the time.
// Called when player collides with enemy flag. 
public void CollectedEnemyFlag()
    {
        if(EnemyFlagCaptured == false)
        {
            EnemyFlagCaptured = true;
        }
    
    }
//Called when the player collides with it's flag. Essentially, it checks to see if that code up there was executed or not.
    public void CheckCapture()
    {
        if(EnemyFlagCaptured == true)
        {
            Debug.Log("Enemy Flag Brought To base! Well done!");
            EnemyFlagCaptured = false;
           //This code returns the results of the check. Returns a boolean value.
            _FlagScript.RetunCheck(EnemyFlagCaptured);
        }
    }
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FlagsScript : MonoBehaviour
{
    [SerializeField]
    private int flagID;
    private PlayerScript Playerscript;
    [SerializeField]
    private bool EnemyFlagCaptured;

    private void Start()
    {
        Playerscript = GameObject.Find("Player").GetComponent<PlayerScript>();
    }
    void Update()
    {
        
    }
//All that stuff up there (should) be irrelevant.
    private void OnTriggerEnter(Collider other)
    {
//This code checks to see what flag was collided with.
        if(other.tag == "Player")
        {
        if(flagID == 1)
        {
                Debug.Log("Collided with your own flag!");
                Playerscript.CheckCapture();
        }
        else if(flagID == 2)
            {
                Debug.Log("Collided with enemy flag");
                EnemyFlagCaptured = true;
                StartCoroutine(FollowPlayer(other.transform));
                Playerscript.CollectedEnemyFlag();
               //I thought repeated collisions were my issue. Guess they weren't because this code disabled collisions.
                 gameObject.GetComponent<CapsuleCollider>().isTrigger = false;
                gameObject.GetComponent<Rigidbody>().detectCollisions = false;
                
                
            }
        }
    }




//The coroutine I am attempting to stop.
    IEnumerator FollowPlayer(Transform PlayerTransform)
    {
        while(EnemyFlagCaptured == true)
       {
            transform.position = PlayerTransform.position;
           yield return new WaitForSeconds(0.05f);
            Debug.Log(EnemyFlagCaptured + "");





        }
        Debug.Log("Flag returned!");
        transform.position = new Vector3(-23, 0.8f, 0.6f);
        }
//I believe my issue is here.
    public void RetunCheck(bool FlagCaptured)
    {
        Debug.Log("Flag Confirmed");
        EnemyFlagCaptured = FlagCaptured;
        Debug.Log(EnemyFlagCaptured + "");
    }


}

To provide some more info on the issue, I added a Debug.log to check the boolean value.


As you can see, it does set to false, but then resets to true. This is where my real issue arises. I can’t for the life of me figure out why. I’ve spent hours looking at my scripts, my scene, debugging and everything else I could think of to do.
Any insight you can provide would be appreciated.
:slight_smile:

1 Like

You can stop the co-routine, by calling StopCoroutine and pass in the co-routine you started.

Coroutine coroutine = StartCoroutine(FollowPlayer(other.transform));		
StopCoroutine(coroutine);