Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

OnCollisionEnter Error "message parameter has to be of type: Collision"

Discussion in 'Editor & General Support' started by xkevenx, Apr 7, 2011.

  1. xkevenx

    xkevenx

    Joined:
    Apr 7, 2011
    Posts:
    3
    I have been trying to get get a collsion based recation, eventually I was to use a trigger to manipulate the play seed of an animation. I started with a tutuorial I found here: http://vimeo.com/3870347
    It uses the below script..(which I'm pretty sure was written in a pre-unity 3 version

    function OnCollisionEnter (collisionInfo : Collision) {
    if (collisionInfo.gameObject.tag == "sphere"){
    var randy: int = Random.Range(-200, 200);
    collisionInfo.gameObject.rigidbody.AddForce(Vector 3.up * 500);
    collisionInfo.gameObject.rigidbody.AddForce(Vector 3.right * randy);
    }
    }

    When I am done with the script I get an error that says:
    "Script Error: OnCollisionEnter
    This message parameter has to be of type: Collision"

    If I change this first line to:

    function OnTriggerEnter (collisionInfo : Collider){

    I don't get the error but I still don't get the result that I want.

    I have seen other people with this problem but there doesn't seem to by any clear solution. (see thread "Can't use FPSwalker AND Bouncy Physic Material")

    I have the objects set up like the video says, I don't know what else to do. Please Help
     
  2. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,772
    Well it looks to me like your code in the first sample is correct. There'll be something else going on.

    just as a test try making a new script with nothing in it except for

    Code (csharp):
    1.  
    2. function OnCollisionEnter(collision : Collision) {
    3.       Debug.Log("I'm working!");
    4. }
    5.  
    If that works then try rebuilding the script bit by bit until it breaks.
     
  3. xkevenx

    xkevenx

    Joined:
    Apr 7, 2011
    Posts:
    3
    So the solution is to name the script something other than "collision". I'm not sure why that is a problem but it is.

    Thanks for the help.
     
  4. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,772
    Oh right.
    The problem is that you calling your script Collision conflicts with Unity's definition of Collision. Because your script is more local to the function you were using it assumes that you were declaring your script Collision as the parameter not the Unity's Collision.

    It's a good idea to avoid naming conflicts if you can but if you can't you can prepend the namespace of the object to get rid of any ambiguity like this:

    Code (csharp):
    1. function OnCollisionEnter(collision : UnityEngine.Collision) {
    2.       Debug.Log("I'm working!");
    3. }
     
    Prathmesh19001 and AceOfHeartsX3 like this.
  5. DanyalKazmi91

    DanyalKazmi91

    Joined:
    Aug 24, 2017
    Posts:
    1
    In OnCollisionEnter2D function the parameter must be other than Collision2D
     
    jonahvolkmann likes this.
  6. katalinkapa

    katalinkapa

    Joined:
    Apr 7, 2021
    Posts:
    1
    Dont know man isnt working..
     
  7. Adelpha

    Adelpha

    Joined:
    Mar 24, 2021
    Posts:
    3
    Same problem your ways don't work
     
  8. Jihaysse

    Jihaysse

    Joined:
    Mar 29, 2020
    Posts:
    53