Search Unity

Issue with OnCollisionEnter & LoadScene.SceneManager

Discussion in 'Getting Started' started by Norton084, Jun 13, 2019.

  1. Norton084

    Norton084

    Joined:
    May 18, 2012
    Posts:
    79
    Hello, I got a tiny problem, I've been working on a little project to learn c# and unity with it. I'm doing a simple 3D dodge the block game, to save myself the time of manually placing blocks and making it quite boring to know where is what, I created procedural spawning points for blocks, they work fine, however, ever since I introduced OnCollisionEnter which is supposed to work with SceneManager to restart the scene when collision is registered things don't seem to want to work anymore.



    The issue is simple, when I press play my blocks spawn, but I'm in a constant loop of scene restarts?

    The codes are below:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     public Rigidbody rb;
    8.  
    9.     public float forwardForce = 1000f;
    10.     public float sidewaysForce = 100f;
    11.  
    12.  
    13.     void FixedUpdate()
    14.     {
    15.        
    16.         rb.AddForce(0, 0, forwardForce * Time.deltaTime);
    17.  
    18.         if (Input.GetKey("d"))
    19.         {
    20.             rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
    21.         }
    22.  
    23.         if (Input.GetKey("a"))
    24.         {
    25.             rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
    26.         }
    27.     }
    28.  
    29.     void OnCollisionEnter()
    30.     {
    31.         FindObjectOfType<GameManager>().EndGame();
    32.     }
    33. }
    34.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class GameManager : MonoBehaviour
    7. {
    8.     public void EndGame ()
    9.     {
    10.         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    11.     }
    12. }
    Would really appreciate if I could get some help on whats causing it.
    Thank you!
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You probably have some object registering a collision you do not expect to. Try outputting the name of the object to the console which is calling OnCollisionEnter. Generally in OnCollisionEnter you should be checking for specific objects which trigger some behavior, instead of triggering that behavior when anything calls it.
     
  3. Norton084

    Norton084

    Joined:
    May 18, 2012
    Posts:
    79
    I've tried doing that by adding debug.log into OnCollisionEnter but nothing logs back?
     
  4. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    Have you added Collision other or Collider other to the OnCollisionEnter?
     
    Joe-Censored likes this.
  5. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    Also just try logging a string on collision like Debug.Log("Hit something");
     
    Joe-Censored likes this.
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Change OnCollisionEnter to the following:

    Code (csharp):
    1.  
    2.     void OnCollisionEnter(Collision collision)
    3.     {
    4.         Debug.Log("Collision with " + collision.gameObject.name);
    5.         //FindObjectOfType<GameManager>().EndGame();
    6.     }
    7.  
     
  7. Norton084

    Norton084

    Joined:
    May 18, 2012
    Posts:
    79
    Done exactly that, yet no log is coming in at all.
     
  8. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    log at endgame perhaps?
     
  9. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    is endgame being called anywhere else?
     
  10. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Well, if you did that exactly and your scene keeps restarting, then the problem has nothing to do with this OnCollisionEnter.
     
    Green11001 likes this.
  11. Norton084

    Norton084

    Joined:
    May 18, 2012
    Posts:
    79
    EndGame is only called in PlayerCollision and GameMaster defines it
     
  12. Norton084

    Norton084

    Joined:
    May 18, 2012
    Posts:
    79
    Problem is, when I take out OnCollisionEnter it works perfectly fine, it only happens when OnCollisionEnter is enabled. Then theres definitely some sort of collision happening?
     
  13. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    check if debug.log is working in start function maybe?
     
    Joe-Censored likes this.
  14. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    In the code I posted it comments out calling EndGame, so if EndGame is getting called then you're calling it somewhere else.

    Edit: Also make sure you haven't disabled messages in your console.
     
    Norton084 and Green11001 like this.
  15. Norton084

    Norton084

    Joined:
    May 18, 2012
    Posts:
    79
    Well, now I feel retarded... The message in my console was disabled, now it 100% states this:

    Collision with Ground
    UnityEngine.Debug:Log(Object)
    PlayerMovement:OnCollisionEnter(Collision) (at Assets/Scripts/PlayerMovement.cs:35)

    There's the culprit, now I gotta find which object collides with ground
     
  16. Green11001

    Green11001

    Joined:
    Apr 14, 2018
    Posts:
    397
    Oh. Put a tag on all your enemy objects and check if "gameobject.tag == enemytag" before calling Endgame
     
    Norton084 and Joe-Censored like this.
  17. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    This ^^^ (but use CompareTag)

    https://docs.unity3d.com/ScriptReference/Component.CompareTag.html

    As I mentioned in my first comment, it is a bad idea to just do something on any collision. You should check what you've collided with first. Otherwise you'll invariably end up with collisions which you don't want to trigger that behavior, but do so anyway.
     
    Norton084 likes this.
  18. Norton084

    Norton084

    Joined:
    May 18, 2012
    Posts:
    79
    Got that fixed.
    Thank you very much guys, I really appreciate it... Literally been thinking all day at work on what I broke..
     
    Joe-Censored likes this.