-1
\$\begingroup\$

I was trying following things.

GameManager :

bool gameHasEnded = false;
public GameObject completeLevelUI;

public void CompleteLevel(){
    completeLevelUI.SetActive(true);    
}

public void EndGame(){
    if(gameHasEnded == false){
        gameHasEnded = true;
        Invoke("Restart",2f);
    }
}

void Restart(){
    SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}

enter image description here

enter image description here

EndTrigger :

public GameManager gameManager;

void OnTriggerEnter(){
    gameManager.CompleteLevel();
}

I get a log in the console "END". Earlier I was using Console.Log("END"); on CompleteLevel(), but I changed that source code. The old code seems to be working and not the new one. It's like Unity couldn't refresh that source code. (And I am sure that I have successfully saved the C# file.)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerCollision : MonoBehaviour
{

    public PlayerMovement movement;

    void OnCollisionEnter(Collision collisionInfo)
    {
        Debug.Log(collisionInfo.collider.name);
        if (collisionInfo.collider.tag == "obstacle")
        {
            movement.enabled = false;
            FindObjectOfType<GameManager>().EndGame();
        }
    }

}
```
\$\endgroup\$
7
  • \$\begingroup\$ Hi there, silly question maybe, but since your old code code is still executed, but you made sure to save the C# file, did you restart the game by exiting play mode and re-entering it? \$\endgroup\$ Commented Feb 10, 2021 at 7:23
  • \$\begingroup\$ @D.Kallan Yes! I did. I tried twice times \$\endgroup\$ Commented Feb 10, 2021 at 7:25
  • \$\begingroup\$ What is calling EndGame? Something in your UI? Is your GameManager getting destroyed on Scene reload or is something resetting gameHasEnded back to true? Are you sure the END is not coming from a different script? Like the one that prints ground \$\endgroup\$ Commented Feb 10, 2021 at 8:15
  • \$\begingroup\$ @Zibelas Yes! You were right. When I double clicked the END log then, it returned me to PlayerCollision. \$\endgroup\$ Commented Feb 10, 2021 at 8:20
  • 2
    \$\begingroup\$ because it uses the wrong method OnTriggerEnter() is missing the parameter of the collision OnTriggerEnter(Collider other). If is has not the parameter, Unity is not calling it on a collision since it can not magically know that it should use it for collision \$\endgroup\$ Commented Feb 10, 2021 at 9:02

1 Answer 1

1
\$\begingroup\$

As Zibelas said in the comments, you are using the incorrect method in OnTriggerEnter(). By not adding anything into the parentheses, the program doesn't know when it should activate the method, thus causing it not to do anything, because it isn't being called. In order to fix this, simply replace OnTriggerEnter() with OnTriggerEnter(Collider other). This will tell it when to run the method.

\$\endgroup\$

You must log in to answer this question.