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 Matt 21 · May 01, 2011 at 03:22 PM · triggereventcoins

Collecting x amount of items triggers event?

So, i have a points and pick up script, the things collected have the tag of Coins. When i collect x amount of coins i want to trigger an event such as when character collects 10 'coins', the wall of cubes disappears? I have no idea how to do this.

My pick up code is:

var score = 0; var scoreText = "Score: 0"; var mySkin : GUISkin;

function OnTriggerEnter( other : Collider ) { Debug.Log("OnTriggerEnter() was called"); if (other.tag == "Coin")

{ Debug.Log("Other object is a coin"); score += 1; scoreText = "Score: " + score; Debug.Log("Score is now " + score); Destroy(other.gameObject); } else { score +=5; scoreText = "Score: " + score; Debug.Log("Score is now " +score); Destroy(other.gameObject); } }

function OnGUI () { GUI.skin = mySkin; GUI.Label (Rect (10, 10, 500, 200), scoreText.ToString()); }

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

2 Replies

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

Answer by karl_ · May 01, 2011 at 03:55 PM

Place the score check at the end of your OnTriggerEnter function, and set the object you'd like to destroy in the inspector.

var wall : GameObject;

// Check if over X amount and if so destroy the object set as 'wall' if(score >= 10) Destroy(wall);

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 Matt 21 · May 01, 2011 at 04:07 PM 0
Share

I've just tried, that and it isn't destroying the item defined as the wall.

avatar image
0
Best Answer

Answer by GesterX · May 01, 2011 at 03:32 PM

You must have a function to "pick up a coin". By your "points" I assume that every time a coin is picked up a point is added.

So in your function which "adds a point" you would want to do a check to see how many total points the player has. In this code I'm assuming your player coin count is a var called "playerCoins":

function pickUpCoin()
{
  //all your coin picking up code
  if (playerCoins == 10)
     {
       DoSomething(); //call the function to do something (destroy wall etc)
     }
}

If you want something more in depth then edit the question and add your current code.

EDIT:

function OnTriggerEnter( other : Collider ) { Debug.Log("OnTriggerEnter() was called"); if (other.tag == "Coin")

 {
 Debug.Log("Other object is a coin");
 score += 1;
 scoreText = "Score: " + score;
 Debug.Log("Score is now " + score);
 Destroy(other.gameObject);
 }
 else
 {
 score +=5;
 scoreText = "Score: " + score;
 Debug.Log("Score is now " +score);
 Destroy(other.gameObject);
 }

if (score >= 10) { //do something } }

Comment
Add comment · Show 5 · 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 Matt 21 · May 01, 2011 at 04:20 PM 0
Share

Sorry i'm new to all of this, where would i put this code? in my pick up script or a new script?

avatar image GesterX · May 01, 2011 at 04:55 PM 0
Share

I've editted my orginal code - so you do your onTriggerEnter stuff and then at the end check if you have more than 10 score. If you do then the code gets run. To destroy a wall just assign the wall to a public GameObject variable like in the answer by the other guy.

avatar image Matt 21 · May 01, 2011 at 05:03 PM 0
Share

Thank you this worked perfectly! $$anonymous$$y brain is fried from all of this lol.

avatar image GesterX · May 01, 2011 at 05:09 PM 0
Share

No worries. Glad to be of help

avatar image Cpullma1 GesterX · Mar 29, 2016 at 10:15 PM 0
Share

What if you want to do something every time the coin count reaches increments of 1000?

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

1 Person is following this question.

avatar image

Related Questions

Similar system to animation events? 0 Answers

Mecanim animation events not working properly 0 Answers

Difference between OnPointerEnter and OnMouseEnter 3 Answers

Generic Triggers and Actions 0 Answers

Assigning events on runtime 0 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