• 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 secondry2 · Mar 30, 2013 at 09:46 AM · itemscollect

Collider Script Not Detecting

Hello!

I've got this script to detect whether the player has picked up an item, and heres the deal: I have a paper entity I imported, and I added a sphere collider, to it, and if the box is trigger is not checked, nothing happens, and the player can move through the paper, and if the is trigger box is checked, the paper never appears, and it says I have one paper. Im new to unity, so forgive some silly mistakes :) var Paper : int = 0; var paperToWin : int = 8;

 function Start() {
 } 
  
 function Update() { 
 
 } 
  
 function OnTriggerEnter( other : Collider ) {
     // Debug.Log("Triggered by [tag] : " + other.gameObject.tag);
     // Debug.Log("Triggered by [name] : " + other.gameObject.name);
     //if (other.gameObject.tag == "dirt"){
        Paper += 1;
        Debug.Log(Paper + "/8 Collected");
        Destroy(other.gameObject);
     //}
 }
  
 function OnGUI()
 {
     if (Paper < paperToWin)
     {
        GUI.Box(Rect((Screen.width/2)-100, 10, 200, 35), "" + Paper + "/8 Collected");
     }
     else
     {
        GUI.Box(Rect((Screen.width/2)-100, 10, 200, 35), "All Papers Collected!");
     }
 }

EDIT: I forgot to mention! The script is a child of the first person controller!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by frarees · Mar 30, 2013 at 10:11 AM

About OnTriggerEnter, documentation says:

This message is sent to the trigger collider and the rigidbody (if any) that the trigger collider belongs to, and to the rigidbody (or the collider if there is no rigidbody) that touches the trigger. Note that trigger events are only sent if one of the colliders also has a rigidbody attached.

As you say, you're attaching that script to a child. Try to attach it to the parent, which should has a rigidbody.

Fast references: http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnTriggerEnter.html http://docs.unity3d.com/Documentation/ScriptReference/Collider.html http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.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 Packetstorm · Mar 30, 2013 at 10:18 AM

cant use functions inside a function, move the function on trigger enter to the end of the script outside the update function and use

 function Update ()
 {
 OnTriggerEnter();
 }
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 secondry2 · Mar 30, 2013 at 09:49 PM 0
Share

I dont understand what you mean. Im pretty sure my OnTriggerEnter function isnt inside the update function

avatar image
0

Answer by secondry2 · Mar 30, 2013 at 10:43 AM

How do I attach a rigidbody to the first person cam controller, and isnt the 1st person cam a parent of the script?

Comment
Add comment · Show 4 · 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 frarees · Mar 30, 2013 at 10:52 AM 0
Share

Actually that's not the best approach for your set-up as I see. If you want a movable character to pick some objects around, you can follow the technique described here: http://answers.unity3d.com/questions/273419/picking-up-a-box.html

avatar image secondry2 · Mar 30, 2013 at 09:48 PM 0
Share

Thats not quite what I was looking for. Aren't I able to do it with this script and a first person controller?

avatar image joeyaaaaa · Mar 30, 2013 at 10:14 PM 0
Share
 function OnCollisionEnter(collision : Collision) {
 for ( var contact : ContactPoint in collision.contacts ) {
 if (contact.otherCollider.name == "Paper"){
 //blahhh
 }
 }
 }

edit can use .tag or . name

avatar image secondry2 · Mar 31, 2013 at 12:21 PM 0
Share

Could you post a tutorial? I can't seem to get it working :(

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

13 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

Related Questions

how can i collect items that fills up a GUI?? 1 Answer

Resouce script help 3 Answers

Android Game crashing when collecting collectibles 0 Answers

Stacking Items in a Inventory List 2 Answers

Item Drop From List Of Six Possible Items Question. C# 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