• 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 Th1rt33nth · Apr 14, 2012 at 03:02 AM · gamescoringpaper

Collecting Papers and Keeping Track of how many collected - need help

Hi, having many issues. This is my code... the furthest I've gotten after a half day trying to get it to work. I have 30 papers scattered around my scene and I want the papers to be destroyed/vanish when the first person controller touches them and I'd like the 'score' to count the number collected - so far this code is only able to display the GUI as 0 Papers - which is great, but I really need help on this.

var papersDisplay : GUIText;

var paper = 0;

var paperToWin = 30;

function Start() {

 DisplayAmount();

}

function DisplayAmount () {

 papersDisplay.text = ""+ paper + " Papers";

}

function OnTriggerEnter( other : Collider ) {

 if (other.tag == "paper") {
 
     paper += 1;
 
     Destroy(other.gameObject);
 }

}

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

1 Reply

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

Answer by AlucardJay · Apr 14, 2012 at 03:20 AM

A few things to check :

  • the statement [other.tag == "paper"] should read [other.gameObject.tag == "paper"]

  • does the gameObject have the tag "paper"

  • does either the player or the paper have a rigidbody component? e.g. Note that trigger events are only sent if one of the colliders also has a rigidbody attached.

  • is either of the object's collider's set to [is Trigger]

From the Unity Scripting Reference : http://unity3d.com/support/documentation/ScriptReference/Collider.OnTriggerEnter.html

  • Edit with code from comments :

       var Paper : int = 0;
         var paperToWin : int = 30;
     
         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 == "Paper"){
                 Paper += 1;
                 Debug.Log("A paper was picked up. Total papers = " + Paper);
                 Destroy(other.gameObject);
             }
         }
     
         function OnGUI()
         {
             if (Paper < paperToWin)
             {
                 GUI.Box(Rect((Screen.width/2)-100, 10, 200, 35), "" + Paper + " Papers");
             }
             else
             {
                 GUI.Box(Rect((Screen.width/2)-100, 10, 200, 35), "All Papers Collected!");
             }
         }
    
    
  • Edit : uploaded an example using this script.

Here is a link to an example : http://www.alucardj.net16.net/unityanswers/PickUpPaper.html

Here is a link to the script on the example : http://www.alucardj.net16.net/unityanswers/ScriptPickUpPaper.js

Comment
Add comment · Show 21 · 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 AlucardJay · Apr 14, 2012 at 03:35 AM 0
Share

Please post comments here

avatar image AlucardJay · Apr 14, 2012 at 03:41 AM 0
Share

ok to check if the the trigger is working , try some Debugging.

put this as the first and second line in the OnTriggerEnter. The console should print some information when the trigger is activated. IF no printout , the trigger is not working.

     Debug.Log("Triggered by [tag] : " + other.gameObject.tag);
     Debug.Log("Triggered by [name] : " + other.gameObject.name);
     
     

also , try setting the collider of the paper to Is Trigger = enabled.

avatar image Th1rt33nth · Apr 14, 2012 at 03:57 AM 0
Share

Tried it - Had a multiple print outs: Triggered by [tag] Untagged, as well as [name] First Person Controller, and [tag]Paper, and [name] PAPER, and [name] lst Floor 002.... so yeah, what does that mean?

avatar image AlucardJay · Apr 14, 2012 at 04:06 AM 0
Share

that's it then =] the trigger is working , but you need to change your if statement. Note the printout shows the papers tag as Paper , not paper. (capitol P). so now :

     if (other.gameObject.tag == "Paper") {
         
         

now you see that you can use a name as well as a tag to check things! Hope this helps =]

avatar image Th1rt33nth · Apr 14, 2012 at 04:20 AM 0
Share

Ok, that totally was the case... gah, it's alway such a tiny error. HOWEVER! Unfortunately... while the papers now vanish, the 'score' is not increasing! Any ideas... Here's the revised code:

Show more comments

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

thrid person help 1 Answer

Plugin Container for Firefox/Unity Web Player crash whenever I try to load Dead Trigger 2 0 Answers

Third Person Controller Question 1 Answer

Me and my friend want to make the same game with unity 2 Answers


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