Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Collision doesnt work

Discussion in 'Documentation' started by mohammedkaabi2017, May 31, 2020.

  1. mohammedkaabi2017

    mohammedkaabi2017

    Joined:
    May 31, 2020
    Posts:
    3
    using UnityEngine;
    public class PlayerCollision : MonoBehaviour
    {
    public PlayerMovement movement;
    void OnCollisonEnter (UnityEngine.Collision collisionInfo)
    {
    if (collisionInfo.gameObject.name == "Obstacle")
    {
    movement.enabled = false;
    }
    }
    }
    when the object i setup collides with the object named "Obstacle", nothing happens, how do i fix it? i tried debugging, and removing the condition but it still doesnt work, there were no console errors
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Hi,
    Your post would be probably more suited to other section of this forum like scripting.
    Have you checked compiler errors in Unity? Although that wrong spelling of OnCollisionEnter is probably the cause.
    You have written "OnCollisonEnter".

    P.S. when you post code, please use code tags.
     
  3. mohammedkaabi2017

    mohammedkaabi2017

    Joined:
    May 31, 2020
    Posts:
    3
    Hello, what is the correct spelling for that? And I have checked there were no compiler errors or console errors. And how do I put my code in code tags?
    Thanks for replying
     
  4. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    If I have to repeat myself, I really wonder if you even looked at what I wrote.

    You have: OnCollisonEnter
    When you should have: OnCollisionEnter
    You are missing one "i".
     
  5. mohammedkaabi2017

    mohammedkaabi2017

    Joined:
    May 31, 2020
    Posts:
    3
    Hey, I got it fixed, but I ran into another problem, which is:

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


    public class PlayerCollision : MonoBehaviour {

    public PlayerMovement movement;

    void OnCollisionEnter(Collision collisionInfo)
    {
    if (collisionInfo.collider.tag== "Obstacle")
    {
    Debug.Log("test");
    movement.enabled = false;
    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);

    }


    }

    }

    it doesnt load the scene i put and i am 100% sure that this is in the right order, but the other two effects are working(the console having the message "test" and the movement becomes disabled)
     
  6. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Please format your code. Edit your post, and add those code tags. Not much asked. If you don't know how, search for it. It's honestly quite frustrating and painful to look at code in plain text.

    Have you added your scene to the build? Try to print out information about that scene you are trying to access and see what you get.