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 starvar · Apr 03, 2012 at 06:45 PM · javaitemsupcoinspick

Pick Up Coins

Hi guys, i tried writing a script to pick up coins so that my score goes up and the coins dissapear but now when i try my main character (named gamer) runs again the coins and keeps running but nothing happens, score doesn't go up or coins don't dissapear, someone knows a script that i could use,

ty in advance.

Comment
Add comment · Show 2
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 save · Apr 03, 2012 at 06:47 PM 0
Share

Post your script and let us help you with what you've got, it's better to learn from your mistakes than getting a finished script that doesn't match your game.

avatar image starvar · Apr 03, 2012 at 06:56 PM 0
Share

static var scoreText:String = "TotalScore: ";

static var score = 0;

function Update ()

{

 scoreText = "TotalScore: " + score;    

}

function OnTriggerEnter( other : Collider ) {

 Debug.Log("OnTriggerEnter() was called");

 if (other.tag == "coin") {

     score += 5;

     scoreText = "TotalScore: " + score;

 }

}

function OnGUI () {

 GUI.Label (Rect (10, 50, 100, 20), scoreText);

 

}

------CODE----- here it is

1 Reply

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

Answer by schaddem · Apr 03, 2012 at 11:12 PM

Your code should work so let's check what could be the problem.

  1. coin does not enter trigger

  2. trigger does nothing

So first look that your debug message does get displayed.

   print("OnTriggerEnter() was called");

If that did not get called check the following:

  1. Coin has a collider/character controller attached to it, and it's enabled

  2. Player has a WORKING collider attached to it which is a TRIGGER

  3. check this with a box collider, see that IsTrigger is set to true (in the inspector) tip: always use simple shapes for testing scripts

  4. If you use a mesh collider (which is probably a bit of a waste for a pickup trigger) it might not work out of the box, so check if it works. Note that a collider can not be something to collide with AND a trigger at the same time(google raycast vs trigger). For a mesh to work as a collider it needs to fulfill certain requirements, it will not work out of the box if all it's faces are not convex. Go to the models import setting, and click on Optimize Mesh(Not sure if it really does but I think that might fix some issues) and more importantly "convex". What "convex" does is, as far as I unterstand, it pretends that the model's faces are convex even if they aren't. Collision might be slightly off, the resulting mesh collider might for example ignore that a bowl has an inside but at least it will work. And for a trigger that's more than good enough.

  5. Check that you have attached your script to the player-object and that its activated. Check that it is running.

If your trigger get's succesfully entered and still nothing happens the problem is probably somewhere between your score-variable and it's output.

To make the coins disappear and check the score change:

 if (other.tag == "coin") {
     Destroy(other);
     print(score);
 
 }


  1. Check that you haven't done something like declaring the coins a trigger. I suspect you're using a character controller or such for the player and have no trigger attached to it. If this is the case add component-physics-box collider to the gamer-object, size it accordingly and click "istrigger".

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 starvar · Apr 04, 2012 at 06:50 AM 0
Share

Forgot the 'is triger' in my box collider, awesome work, ty for your time!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Storing items in inventory 1 Answer

Object pick up script 1 Answer

Picking up an object 2 Answers

Picking up object occasionally causes player to fly through air 0 Answers

Mouse locked in center 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