K
K
Keplers2021-03-14 16:58:54
Unity
Keplers, 2021-03-14 16:58:54

How to remove automatic deletion of an object?

I don’t understand why the image is created after 2 visits, and then destroyed again
, this starts to happen after the scene is reloaded. via method: Call field:
SceneManager.LoadScene(0);

bool attack = true;
  private const string nameTag = "Player";
  private void OnCollisionStay2D(Collision2D collision)
  {
    if (collision.gameObject.CompareTag(nameTag) && attack)
    {
      attack = false;
      Life.Handler(-damage); //Вызов 
      Invoke("Attacking", 1);
    }
  }

  void Attacking() => attack = true;

Issue script:
using UnityEngine;
using UnityEngine.UI;

public class Life : MonoBehaviour
{
  private Image image = null;
  public delegate void HandlerDelegate(float life);
  public static HandlerDelegate Handler;

  public static float Max { get; set; } = 100f;
  public static float Current { get; set; } = 100f;

  private void Start()
  {
    Debug.Log(Current);
    image = transform.GetChild(1).GetComponent<Image>();
    Handler += Operation;
    Handler += Analysis;
  }
  private void Operation(float life)
  {
    float newLife = Current + life;
    Current = newLife <= 0 ? 0 : newLife > Max ? Max : newLife;
  }

  private void Analysis(float life)
  {
    GameOver();
    //if(image != null)
    {
      Debug.Log(Current);
      image.fillAmount = Current / Max; //Ошибка. После перезагрузки сцены, каждый 3й раз происходит инициализация  без ошибки.
    }
  }

  private void GameOver()
  {
    if ((int)Current == 0)
    {
      PanelController.game.SetActive(false);
      PanelController.gameOver.SetActive(true);
    }
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Keplers, 2021-03-14
@Keplers

Solved the problem image made static.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question