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 Katherz · Feb 20, 2013 at 10:51 PM · triggerscoreguitext

Picking up an Item and telling you how many you have

Hey guys, I am really new to Unity and I am having a problem. In my game, I am having the player pick up keys (6 in total) and I want the GUIText to say "Keys Collected: 1/6" and have it increment each time a key is picked up and then have the text disappear. I am so confused on how to do this and Ive been searching to the forums for help but I still don't understand how to do this. I have some code up but it is not working correctly. Please help!

 var keyAlert : GUIText ;
 var key : int = 0;
 function OnTriggerEnter (other : Collider) {
     if (other.gameObject.tag == "Player"){
         key += 1;
         alertKey ();
         Destroy(gameObject);
     }
 }
 function alertKey(){
     keyAlert.text = "Keys Collected:" + key ;
     yield WaitForSeconds (3);
     keyAlert.text = "";
 }
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 hoy_smallfry · Feb 20, 2013 at 11:23 PM 0
Share

Is that script supposed to belong to the keys? I'm getting that impression because the OnTriggerEnter function is checking if the collider tag is the player.

If so, that is one of the fundamental problems.

avatar image Katherz · Feb 20, 2013 at 11:27 PM 0
Share

So I need to take it off the key item then, what can I do to make it trigger and appear? (sorry accidently posted it under answers, I'm new in the forums)

1 Reply

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

Answer by hoy_smallfry · Feb 20, 2013 at 11:42 PM

With scripts in Unity, each GameObject that has a script attached to it has a it's own variable state. that means that Key A has it's own value for "keyAlert" and "key" separate from keys B, C, D, E, F, and vice versa.

The key script you have written is waiting for a collision with the player, but each time there is a collision, you're only updating that key's "key" variable. Then soon after, you destroy the key's GameObject, which destroys all the data attached to it, including your script variables attached to it.

There are two ways to go about this, one gets the job done but has a bad design. The other has a better design and does the same thing.

1) There is a way to share a variable between objects of a type. If you name "key" as a static variable, it is a value where memory is shared between all

 static var key : int = 0;

Then, when one key dies, the shared memory is not destroyed.

2) The previous way works, but its bad design because it goes against an object-oriented design principle called the Single Responsibility Principle. The keys shouldn't do more than be keys. That is, you shouldn't make them both inventory counters and keys, because it just complicates things. So, instead of having the keys check for player collision and checking the inventory count, you should reverse the relationship - the player should be the one doing what this script does.

To rewrite the script so it is no longer a Key script but an Inventory script:

A) Attach it to the player

B) Make all your key GameObjects have a Key tag

C) Make OnTriggerEnter check for "Key" tag instead of "Player" in the if statement.

D) Have it destroy other.gameObject instead, which is the key.

Comment
Add comment · Show 2 · 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 hoy_smallfry · Feb 20, 2013 at 11:57 PM 0
Share

If this answer is the right one for you, you should hit the check mark button and underneath the vote up/down.

avatar image Katherz · Feb 21, 2013 at 12:15 AM 0
Share

Thank you so very much!

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

10 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

Related Questions

Trigger Script Not Firing 4 Answers

Use sprites as score/GUItext? 1 Answer

How can I make triggering multiple triggers give more points? 1 Answer

how do i attach GUITEXT to a prefab 1 Answer

What is the best way to show (Instantiate) a GUIText at the position of a GameObject? 3 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