0
\$\begingroup\$

I'm making a game and when the enemy dies I keep it in memory. I want the person playing to find the game just as they left it when they enter. I'm doing most of these, but there's one small problem left. The problem is, when I write the if block code line in Fixed update, I get the value true when the enemy dies and the true tick is activated in the inspector, but when I want to do this with the coroutine, I cannot activate it. In general, I had trouble using Coroutine with if blocks while making the game. If anyone knows where the problem is, I would be very happy if they could help. How should I revise this code? Where is the mistake?

I call this coroutine at start

using System.Collections;
using UnityEngine;

public class Flags : MonoBehaviour
{
    public GameObject[] SoldierOfFlag;
    public bool[] DiedTF = new bool[7];

    private WaitForSeconds HalfASecond = new WaitForSeconds(0.5f);

    void Start()
    {
        StartCoroutine(SoldierDeadRegistrationCor());
    }

    IEnumerator SoldierDeadRegistrationCor()
    {
        while (true)
        {
            yield return HalfASecond;

            for (int i = 0; i < 7; i++)
            {
                if (SoldierOfFlag[i].GetComponent<BlueSoldiersScript>().Soldier.SoldierLife <= 0 && !DiedTF[i])
                {
                    DiedTF[i] = true;
                }
            }
        }
    }
}

This is enemy soldier

This is enemy soldier

These are the bool values ​​in the inspector window that I expect to be active.

Bool values

In the screenshot here, the enemy soldier dies, but the bool value does not change.

In the screenshot here, the enemy soldier dies, but the bool value does not change.

\$\endgroup\$
11
  • 1
    \$\begingroup\$ This is a not a really good practise the way you are approaching the problem. There should be no need to check every 0.5s for every enemy if it is alive. Repeatingly calling GetComponent<BlueSoldiersScript>() is also expensive. Normally when the enemy dies, it would just report (maybe to a level or enemy manager) that it is dead and you can handle it from there. \$\endgroup\$
    – Zibelas
    Commented Apr 17 at 16:50
  • \$\begingroup\$ I know, when the soldier died, I tried activeself and it didn't work. I chose this costly path from him. Do you know another way? What should I do when a soldier dies? \$\endgroup\$ Commented Apr 17 at 17:14
  • \$\begingroup\$ The soldiers guarding the totem in the inspector window. Is there a line of code in there that you know of that activeself didn't work? \$\endgroup\$ Commented Apr 17 at 17:15
  • \$\begingroup\$ There should be no need to check if the game object is active or not. You will have somewhere in your code a line that handles how much hp it has or that handles the death of the enemy. You just call from there your manager and report: I died. The manager can hold all references to all your units in the scene. Depending if they are in the scene from the beginning or the manager spawns them, you would need to look them only once up \$\endgroup\$
    – Zibelas
    Commented Apr 17 at 17:30
  • \$\begingroup\$ Your TotemKoruyanlar array only has two elements, but you're indexing the array past 1 in your loop. You're probably getting errors in the console. \$\endgroup\$
    – TTTTTTa
    Commented Apr 18 at 4:23

1 Answer 1

1
\$\begingroup\$

Your array TotemKoruyanlar has only 2 elements, but your loop tries to access 7. This will cause an array index out of range exception and stop the coroutine from running. You should see these errors in red in your console window.

The reason it worked for you in FixedUpdate is that, although it will still cause the exception, it won't prevent it running again on subsequent frames.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .