Trying to Invoke method: Player.GameOver couldn't be called.

I don’t understand why you’re not calling…
If anyone can help me, I would appreciate it!

6100800–663429–gamecontroller.cs (461 Bytes)
6100800–663432–Player.cs (2.66 KB)

I don’t understand either. You’re not very clear on what you are doing and how you’re trying to invoke your gameover call. Also not, it’s better to post code straight in the forum using code tags, you’ll get more help that way. That being said your Player has a private GameOver method, but how is that being triggered?

 Player.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    private CharacterController controller;

    public float speed;
    public float jumpHeight;
    private float jumpVelocity;
    public float gravity;

    public float rayRadius;
    public LayerMask layer;

    public float horizontalSpeed;
    private bool isMovingLeft;
    private bool isMovingRight;

    public Animator anim;
    private bool isDead;

    private GameController gc;

    // Start is called before the first frame update
    void Start()
    {
        controller = GetComponent<CharacterController>();
        gc = FindObjectOfType<GameController>();
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 direction = Vector3.forward * speed;

        if (controller.isGrounded)
        {
            if(Input.GetKeyDown(KeyCode.Space))
            {
                jumpVelocity = jumpHeight;
            }

            if(Input.GetKeyDown(KeyCode.RightArrow) && transform.position.x < 3f && !isMovingRight)
            {
                isMovingRight = true;
                StartCoroutine(RightMove());
            }

            if(Input.GetKeyDown(KeyCode.LeftArrow) && transform.position.x < -3f && !isMovingLeft)
            {
                isMovingLeft = true;
                StartCoroutine(LeftMove());
            }
        }
        else
        {
            jumpVelocity -= gravity;
        }

        OnCollision();

        direction.y = jumpVelocity;

        controller.Move(direction * Time.deltaTime);
    }

    IEnumerator LeftMove()
    {
        for(float i = 0; i < 10; i += 0.1f)
        {
            controller.Move(Vector3.left * UnityEngine.Time.deltaTime * horizontalSpeed);
            yield return null;
        }
        isMovingLeft = false;
    }

    IEnumerator RightMove()
    {
        for(float i = 0; i < 10; i += 0.1f)
        {
            controller.Move(Vector3.right * UnityEngine.Time.deltaTime * horizontalSpeed);
            yield return null;
        }
        isMovingRight = false;
    }

    void OnCollision()
    {
        RaycastHit hit;
        if(Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, rayRadius, layer) && !isDead)
        {
            //CHAMA O GAME OVER
            anim.SetTrigger("die");
            speed = 0;
            jumpHeight = 0;
            horizontalSpeed = 0;
            Invoke("GameOver", 1.0f);

            isDead = true;
        }
    }

    void GamerOver()
    {
        gc.ShowGamerOver();
    }
}
 gamecontroller.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameController : MonoBehaviour
{
    public GameObject gameOver;

    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
       
    }

    public void ShowGamerOver()
    {
        gameOver.SetActive(true);
    }
}

I’m not very good at C# but I got private to stay in the code. Besides, I’m trying to call gc.ShowGamerOver, without Invoke I could call him but I would like to make a time between the death animation and the GamerOver panel so I used Invoke. It was not very confusing but my English.

Instead make GameOver it’s own script. Then update the Timer with Time.deltaTime and allow the Update to continue this until enough time passes.

public class GameOver : MonoBehaviour
{
    public float Timer = 0;
    float duration = 1; //1 second
    void Update()
    {
        if(Timer>0)
        {
           Timer += Time.deltaTime;
           if(Timer>= duration)
           {
               //enough time has passed so do something like displaying game over screen
             
               //reset
               Timer = 0;
           }
        }
    }
}

Well, and here is the problem with Invoke and why I don’t suggest people use it as there is no compile time checking. (especially if you’re new to programming)

Look closer at what you have. Your invoke call string is “GameOver”
However, your method is GamerOver.

Do you see the difference?

Thus your invoke call looks for GameOver and can’t find it. I’d figure it would throw an error, but since I don’t use it, not sure what it does when it can’t find a matching method.

1 Like