Search Unity

void OnCollisionEnter2D(Collision2D other) doesnt work!

Discussion in '2D' started by campout, Aug 9, 2019.

  1. campout

    campout

    Joined:
    Aug 9, 2019
    Posts:
    5
    Hi i'm pretty new to programming and was following a tutorial on making 2D rpg games. I got to
    void OnCollisionEnter2D(Collision2D other) {}, but the problem is my 2 objects (a player and a slime) are colliding but I don't think its calling this method when it collides. Any ideas?

    Code (CSharp):
    1. void OnCollisionEnter2D(Collision2D other)
    2.         {
    3.             transform.gameObject.SetActive(false);
    4.             if (other.gameObject.name == "Player")
    5.             {
    6.                 other.gameObject.SetActive(false); //is it active or not? set it to false
    7.                 reloading = true;
    8.                 thePlayer = other.gameObject;
    9.             }
    10.  
    11.         }
    Screenshot (419).png Screenshot (420).png
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
    Looks like you are setting the gameObject to false when it collides with anything. Then, after it detects the player you are doing again. Delete the first time you do it and it should work. You have Rigidbody2D and Collider2Ds on both object so that is good.
     
  3. campout

    campout

    Joined:
    Aug 9, 2019
    Posts:
    5
    Thank you for the quick reply! Even after deleting the first line, nothing happens when the player collides with the slime.
    Maybe it has something to do with the rest of the code?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SlimeController : MonoBehaviour
    6. {
    7.     public float moveSpeed;
    8.     private Rigidbody2D myRigidbody;
    9.     private bool moving;
    10.     public float timeBetweenMove;
    11.     private float timeBetweenMoveCounter;
    12.     public float timeToMove;
    13.     private float timeToMoveCounter; //the time when set will be 0, we need a way to reset the time
    14.     private Vector3 moveDirection;
    15.     public float waitToReload;
    16.     private bool reloading;
    17.     private GameObject thePlayer;
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         myRigidbody = GetComponent<Rigidbody2D>();
    22.         //timeBetweenMoveCounter = timeBetweenMove;
    23.         //timeToMoveCounter = timeToMove;
    24.         timeBetweenMoveCounter = Random.Range(timeBetweenMove *0.75f, timeBetweenMove * 1.25f);
    25.         timeToMoveCounter = Random.Range(timeToMove * 0.75f, timeToMove * 1.25f);
    26.  
    27.  
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Update()
    32.     {
    33.         if (moving)
    34.         {
    35.             timeToMoveCounter -= Time.deltaTime;
    36.             myRigidbody.velocity = moveDirection;
    37.             if (timeToMoveCounter < 0f)
    38.             {
    39.                 moving = false;
    40.                 //timeToMoveCounter = timeToMove;
    41.                 timeToMoveCounter = Random.Range(timeToMove * 0.75f, timeToMove * 1.25f);
    42.  
    43.             }
    44.         }
    45.         else
    46.         {
    47.             timeBetweenMoveCounter -= Time.deltaTime;
    48.             myRigidbody.velocity = Vector2.zero;
    49.             if (timeBetweenMoveCounter < 0f)//cuz what if it's not zero?
    50.             {
    51.                 moving = true;
    52.                 //timeBetweenMoveCounter = timeToMove;
    53.                 timeBetweenMoveCounter = Random.Range(timeBetweenMove * 0.75f, timeBetweenMove * 1.25f);
    54.                 moveDirection = new Vector3(Random.Range(-1f, 1f) * moveSpeed, Random.Range(-1f, 1f) * moveSpeed, 0f);
    55.  
    56.             } //enemy comes online, it's not moving. when counter reaches 0, it starts moving again in a random direction. Repeat
    57.             if (reloading)
    58.             {
    59.                 waitToReload -= Time.deltaTime;
    60.                 if (waitToReload < 0)
    61.                 {
    62.                     Application.LoadLevel(Application.loadedLevel);//loaded level is the level that we is loaded right now
    63.                     thePlayer.SetActive(true);
    64.                 }
    65.             }
    66.         }
    67.  
    68.  
    69.         void OnCollisionEnter2D(Collision2D other)
    70.         {
    71.            
    72.             if (other.gameObject.name == "Player")
    73.             {
    74.                 other.gameObject.SetActive(false); //is it active or not? set it to false
    75.                 reloading = true;
    76.                 thePlayer = other.gameObject;
    77.             }
    78.  
    79.         }
    80.     }
    81. }
    82.  
     
  4. campout

    campout

    Joined:
    Aug 9, 2019
    Posts:
    5
    I also used a console message in void OnCollisionEnter2D(Collision2D other) as the first line, but it never prints. I think somehow collision isn't being sensed.
     
  5. campout

    campout

    Joined:
    Aug 9, 2019
    Posts:
    5
    OnTrigger works fine ,but collision doesnt work
     
  6. campout

    campout

    Joined:
    Aug 9, 2019
    Posts:
    5
    Assets\Scripts\SlimeController.cs(69,14): warning CS8321: The local function 'OnCollisionEnter2D' is declared but never used. I also get this warning
     
  7. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
    It looks like you are calling OnCollisionEnter2D inside of Update. Move that collision function between lines 80 and 81.