Coroutines not properly working

It’s like they’re running on the same thread, this is the code I made

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using RAIN.Core;
using RAIN.Action;
using RAIN.Memory;

public class combatStatusChanges : MonoBehaviour {

    public AudioClip weaponShot;
    public GameObject rayOrigin;
    public GameObject target;
    AIGunHandling AIGunReference;
    public AIRig currentAI;
    bool isFiring = false;
    public static bool startEngaging = false;
    MemoryObject isInCover;
    MemoryObject targetNull;
    bool canChangeBehavior = true;

    // Use this for initialization
    void Start () {
        currentAI.AI.Mind.AI.WorkingMemory.SetItem<bool>("fire", false);
    }
   
    // Update is called once per frame
    void Update () {
        targetNull = currentAI.AI.Mind.AI.WorkingMemory.GetItem("varHero");
        isInCover = currentAI.AI.Mind.AI.WorkingMemory.GetItem("closeCover");
        Debug.Log(targetNull);
        if(currentAI.AI.Senses.GetSensor("Eyes").AI.WorkingMemory.GetItem("varHero") != null)
        {
            if(UnityEngine.Random.Range(0, 100) >= 50 && canChangeBehavior)
            {
                currentAI.AI.Mind.AI.WorkingMemory.SetItem<bool>("canFire", true);
                if(!isFiring)
                    StartCoroutine(fire());
                StartCoroutine(waitForBehaviorChange());
            }
            else if(canChangeBehavior)
            {
                currentAI.AI.Mind.AI.WorkingMemory.SetItem<bool>("canFire", false);
                StartCoroutine(waitForBehaviorChange());
            }
        }
    }

    IEnumerator waitForBehaviorChange()
    {
        canChangeBehavior = false;
        yield return new WaitForSeconds(2f);
        canChangeBehavior = true;
    }

    IEnumerator fire()
    {
        isFiring = true;
        transform.LookAt(target.transform);
        Ray ray = new Ray(rayOrigin.transform.position, transform.forward);
        RaycastHit hit;
        audio.PlayOneShot(weaponShot);
        Debug.DrawRay(ray.direction, transform.position);
        if(Physics.Raycast(ray, out hit, 2000))
        {
            if(hit.collider.tag == "Player")
            {
                Debug.Log("Player hit");
            }
        }
        yield return new WaitForSeconds(1 / 14);
        isFiring = false;
    }
}

And when the waitForBehaviorChange() coroutine is called the fire() coroutine is paused until the first one is done, in fact the model looks at the player every 2 seconds.
How can I fix this? Thanks for the help.

Infact coroutines are running on the mainthread. Coroutines DONT run on another thread. They are just methods which get paused for a certain time (or frame) and than get continued at another point in time.

As of your question, you may rephase it since I dont understand what the problem is. :wink:

Alright :stuck_out_tongue:

I want the AI to make a decision by using Random.Range, if the AI shoots then I use a coroutine to give the gun a rate of fire (1/14) so it doesn’t shoot each frame but at the same time I don’t want the AI to instantly change action otherwise it would always keep getting inside cover and shooting, it would be ugly and it wouldn’t work.
Anyway, when the AI starts to fire another coroutine is called to avoid it changing action (waitForBehaviorChange) the problem is that when waitForBehaviorChange is called fire() is paused and the AI instead of shooting every 0.07 seconds it shoots every 2 seconds because it can change behavior after 2 secs.
Hope that was more clear, thanks again for the help!

Now that I think of it, would starting the coroutine in another script work?

Anyone? What if I use Time.deltaTime? WOuld that properly work?