function OnCollisionEnter does not work

In my game, the Player picks-up apples (with OnTriggerEnter) on a maximum of 5 each time and after that he should deposit them in a basket or depository (with OnCollisionEnter) where he can place a total of 10 apples (he has to make two trips for that). On each completed load the GUI should show the pick-up apples and zeroing the carried apples. My picks-ups work just fine but I should have something wrong with the deposit on the basket because OnCollisionEnter is not working.

Can you help me on this? This is my script:

    var carrying : int;
    var carryLimit : int;
    var deposited : int;
    var winScore : int;
    var appleCollect : AudioClip;
    var appleDeposited : AudioClip;
    var carriedGui : GUIText;                       
    var depositedGui : GUIText;
    var guiMessage : GameObject;        
    var collisionInfo : Collider;
    var depository : GameObject;

    function Start()
    {
        UpdateCarryingGui();
        UpdateDepositedGui();

    }

    function UpdateCarryingGui()
    {
            carriedGui.text = "Carrying: " + carrying + " of " + carryLimit;
    }

    function UpdateDepositedGui()
    {
            depositedGui.text = "Deposited: " + deposited + " of " + winScore;  
    }

    function OnTriggerEnter(collisionInfo : Collider)
    {   
        if (collisionInfo.gameObject.tag == "apple"){
            carrying++;
    }
        if ( carrying <= carryLimit ){

        UpdateCarryingGui();
        audio.PlayOneShot(appleCollect);
        Destroy(collisionInfo.gameObject);
       }    
            if ( carrying >= carryLimit )
       {
            var warning : GameObject = Instantiate( guiMessage );
            warning.guiText.text = "You can't carry any more apples, go to the basket";
            Destroy(warning, 2);
       }
    }
print("TEST1");

    function OnCollisionEnter(depositing : Collision)
   {    
    if (depositing.gameObject.tag == "depository" && carrying >= carryLimit)
    {deposited += carrying;       
    carrying = 0;
    UpdateCarryingGui();
    UpdateDepositedGui();
    audio.PlayOneShot(appleDeposited);

print("TEST2");

    }
}

4 Answers

4

Is your depository object literally tagged "depository"? If it's not, it won't work.

Yes Sir! It is a cube collider, it is tagged depository, it is checked as trigger. Is this correct?

–

I just added two print tests, one before OnCollisionEnter an other at the end of it. The first shows OK, the second does not appears. Obviously for some reason the function OnCollisionEnter is not executing here. I do not understand what is happening.

–

Although this looks like a monologue with myself, I continue explaining my progress in the hope that some one could help me.

I decided not to use OnCollisionEnter but using the previous called OnTriggerEnter for the part that is giving me problems.

My script now looks like this:

var carrying : int;
var carryLimit : int;
var deposited : int;
var winScore : int;
var appleCollect : AudioClip;
var appleDeposit : AudioClip;
var carriedGui : GUIText;                       
var depositedGui : GUIText;
var guiMessage : GameObject;        
private var timeSinceLastPlay : float;  
var collisionInfo : Collider;
var depository : Collider;
var collision : Collider;

function Start()
{   
        timeSinceLastPlay = Time.time;
    UpdateCarryingGui();
    UpdateDepositedGui();   
}

function UpdateCarryingGui()
{
        carriedGui.text = "Carrying: " + carrying + " of " + carryLimit;
}

function UpdateDepositedGui()
{
        depositedGui.text = "Deposited: " + deposited + " of " + winScore;  
}

function OnTriggerEnter(collisionInfo : Collider)
{   
    if (collisionInfo.gameObject.tag == "apple")
{
        carrying++;
}
    if ( carrying <= carryLimit )

{
    UpdateCarryingGui();
    audio.PlayOneShot(appleCollect);
    Destroy(collisionInfo.gameObject);
}   
        if ( carrying >= carryLimit )
{
        var warning : GameObject = Instantiate( guiMessage );
        warning.guiText.text = "You can't carry any more apples, go to the basket";
        Destroy(warning, 2);
}

    if (depository.gameObject.tag == "depository" && carrying >= carryLimit)
   {
    deposited += carrying;       
    carrying = 0;
    UpdateCarryingGui();
    UpdateDepositedGui();
    audio.PlayOneShot(appleDeposit);
   }
}

This script is attached to my Player. As I told previously, everything works fine until the moment when the Player should go to the basket to deposit the 5 collected apples. In that moment the line

if (depository.gameObject.tag == "depository" && carrying >= carryLimit)

should be called and I get the problem. I decided to make visible the depository collider box mesh in the game and I observed that it is destroyed when the Player touches it. Then a question arises : the function OnTriggerEnter that I used previously to pickup the apples ( with the instruction of destroy them after picking them up) is also destroying the depository. How can I resolve this?

The depository's getting destroyed because the condition "carrying <= carryLimit" is true. Try encasing that whole 'if' block - and the one after it, really - inside the test for 'tag == "apple"'.

–

If your player is using a CharacterController, then you should look at the CharacterController.OnControllerColliderHit method.

Thanks Superpig (I did not want to call you like that, ;) ):

I just did this:

  1. Used OnCollisionEnter for the part that is working (picking up apples)

  2. For the depositing part, I used:

function OnControllerColliderHit(hit : ControllerColliderHit)

{ if(hit.gameObject.tag == "depository")

if (carrying >= carryLimit)

{
 deposited += carrying;       
 carrying = 0;
 UpdateCarryingGui();
UpdateDepositedGui();
print("TEST2");
//audio.PlayOneShot(appleDeposit);

} }

But the problem is the same. This could happens because I am using colliders with different names in the same functions?

Will you recommend using CharacterController.OnControllerColliderHit method for both parts (picking up and depositing the apples)?

Can you suggest me a way to do it?

Thanks a lot in advance.

SOLVED!