Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by pepefirst · Apr 24, 2011 at 12:19 AM · ontriggerenteroncollisionenterguitext

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");

 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by pepefirst · Apr 25, 2011 at 02:18 AM

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by e-bonneville · Apr 24, 2011 at 01:09 AM

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pepefirst · Apr 24, 2011 at 01:32 AM 0
Share

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

avatar image pepefirst · Apr 24, 2011 at 03:30 AM 0
Share

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

avatar image
0

Answer by pepefirst · Apr 24, 2011 at 11:30 PM

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" &amp;&amp; carrying &gt;= 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?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image superpig ♦♦ · Apr 24, 2011 at 11:57 PM 0
Share

The depository's getting destroyed because the condition "carrying

avatar image
0

Answer by superpig · Apr 24, 2011 at 11:55 PM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

2 objects collide, need to destroy one 2 Answers

Response speed in OnCollisionEnter / OnTriggerEnter 1 Answer

How to detect collision without OnCollisionEnter or OnTriggerEnter? 1 Answer

How to make multiple gameobjects instantiate from one collider? 3 Answers

OnCollisionEnter doesn't work 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges