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 BrokeBob · Aug 06, 2013 at 01:11 AM · oncollisionenteritem pickup

Item pick up script not working

Hey there. I'm new to Unity, and I'm working on my first simple game. Basically all you have to do is pick up four objects scattered around the area, and a the end screen pops up. The problem is that the script I wrote for it is not working. I attached the script to the player, but when I collide with an object, nothing happens. The counter doesn't go up, and the object isn't destroyed. The script is below, and again I'm very new to this. Don't chop my head off if I made a simple mistake. Thank you for reading, and please help me out.

 #pragma strict
 
 var count : int;
 private var text : GUIText;
 
 function OnCollisionEnter(collision : Collision)
 {
     if(collision.transform.tag == "Finish")
     {
         count += 1;
         Destroy(collision.gameObject);
     }
     print(Time.time);
 }
 
 function Update ()
 {
     if(count >= 4)
     {
         count = -1;
         var g = new GameObject();
         text= g.AddComponent(GUIText);
         text.text = "The End";
         text.transform.position = new Vector3(0.5F,0.5F,0);
         text.anchor = TextAnchor.MiddleCenter;
     }
 }
 
Comment
Add comment · Show 10
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 cdrandin · Aug 06, 2013 at 01:19 AM 1
Share

Have you assigned your objects the appropriate tag?

avatar image clunk47 · Aug 06, 2013 at 04:04 AM 1
Share

You should use proper tagging and formatting for your questions. I've fixed this for you to give it a better chance of being answered properly.

avatar image clunk47 · Aug 06, 2013 at 07:48 PM 1
Share

Is the character a rigidbody or a charactercontroller?

avatar image clunk47 · Aug 07, 2013 at 12:14 AM 1
Share

Have a look at my edited answer below. Character Controller only detects collisions while moving. You either have to use OnControllerColliderHit like in my answer below, or attach a rigidbody to the character.

avatar image Joshua4missions · Aug 07, 2013 at 12:24 AM 1
Share

As a suggestion you should make use of Debug.Log("Log text");

This way you can track how far your script ran before you encountered the problem.

Show more comments

2 Replies

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

Answer by clunk47 · Aug 06, 2013 at 04:11 AM

Be sure the tag is correct, make sure you have a rigidbody attached to one of the objects as well. Also be certain neither collider is a trigger.

From the script reference, "Note that collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached.".

But if you are using a CharacterController, you need to use OnControllerColliderHit

[1]:http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html

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 Griffo · Aug 06, 2013 at 06:32 AM

Is your "Finish" GameObjects collider "is Trigger" ticked in the inspector?

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 BrokeBob · Aug 06, 2013 at 10:45 PM 0
Share

I've tried it both ways, but to be honest, I don't know if it should be.

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

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why does my player get pushed away when and after an enemy is spawned 0 Answers

OnCollisionEnter not working. 2 Answers

How to succesfully implement OnCollisionEnter? 2 Answers

Which Object Calls OnCollisionEnter()? 3 Answers

Physics Collision Only Seems to Affect Moving Objects 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