Answer the question
In order to leave comments, you need to log in
Error: Trying to Invoke method: Player.Lose couldn't be called?
Good day, I want to activate the return of my character after death. That is, so that the game automatically restarts. But it pops up - "Trying to Invoke method: Player.Lose couldn't be called". I tried to find an error in the code, but without success. I will be glad if you help)
The first part of the code:
< code lang = "cs" >
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Main : MonoBehaviour
{
public void Lose()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
The second part of the code:
< code lang = "cs" >
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehavior
{
Rigidbody2D rb;
public float speed;
public float jumpHeight;
public Transform groundCheck;
bool isGrounded;
Animator anim;
int cur HP; //current health point (current number of lives)
int maxHp = 3; //maximum number of lives
bool isHit = false;
public main main;
void Start()
{
rb = GetComponent();
anim = GetComponent();
curHp = maxHp;
}
void Update()
{
CheckGround();
if (Input.GetAxis("Horizontal") == 0 && (isGrounded))
{
anim.SetInteger("State", 1);
}
else
{
Flip();
if (isGrounded)
anim.SetInteger("State", 2);
}
}
void FixedUpdate()
{
rb.velocity = new Vector2(Input.GetAxis("Horizontal") * speed, rb.velocity.y);
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
rb.AddForce(transform.up * jumpHeight, ForceMode2D.Impulse);
}
void Flip()
{
if (Input.GetAxis("
transform.localRotation = Quaternion.Euler(0, 0, 0);
if (Input.GetAxis("Horizontal") < 0)
transform.localRotation = Quaternion.Euler(0, 180, 0);
}
void CheckGround()
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(groundCheck.position, 0.2f);
isGrounded = colliders.Length > 1;
if (!isGrounded)
anim.SetInteger("State", 3);
}
public void RecountHp(int deltaHp)
{
curHp = curHp + deltaHp;
if (deltaHp < 0)
{
StopCoroutine(OnHit()); // so that coroutines do not overlap each other
isHit = true; // and there was no competing value between
StartCoroutine(OnHit()); // green and blue values
}
print(curHp);
if (curHp <= 0)
{
//.enabled allows you to disable or enable any component of the object
GetComponent().enabled = false;
Invoke("Lose", 2f);
}
IEnumerator OnHit()
{
if (isHit) // Caroutine starts first
GetComponent().color = new Color(1f, GetComponent().color.g - 0.04f, GetComponent().color.b - 0.04f);
// then goes to red
else
GetComponent().color = new Color(1f, GetComponent().color.g + 0.04f, GetComponent().color.b + 0.04f);
// after isHit = false, we will reach the standard color and get to it, and after that the condition will be checked again, again and again (while g == 1f)
if (GetComponent().color.g == 1f)
StopCoroutine(OnHit ());
if (GetComponent().color.g <= 0) // when we hit red we do
isHit = false; // then we do isHit false
yield return new WaitForSeconds(0.02f);
StartCoroutine(OnHit());
}
void Lose()
{
main.GetComponent().Lose();
}
}
}
Answer the question
In order to leave comments, you need to log in
StopCoroutine(OnHit());
Incorrect use. In fact, nothing stops you.
Therefore, it Lose
is called many times and eventually when the object no longer exists.
I don't know how useful it will be for you now, a year later. But you are trying to run a method in another method.
It was necessary to go a couple of brackets below, and everything is in openwork!
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question