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 Tijs · Dec 29, 2012 at 05:17 PM · guiobjectdestroylivespick up

Pick up objects

In my 2D Game my player can lose and pick up lives. at this point, i can endlessly add lives, by bumping in to my life-object. So i want the object to disappear when i take it.

How do i do this?

if(hit.gameObject.tag == "dead") { dead = true; HealthControl.LIVES -= 1; }

if(hit.gameObject.tag == "life") { Destroy(life); HealthControl.LIVES += 1;}

Comment
Add comment · Show 1
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 amayasan013261 · Jul 06, 2015 at 01:54 AM 0
Share

what does 'lives' stand for? and what kind of variable is healthcontrol?

2 Replies

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

Answer by Lovrenc · Dec 29, 2012 at 05:33 PM

Even though your code should work i think this is not a good practice. Your player script seems to know about all the events and items in your game.

Suppose you want to add some other "potions and buffs". This would in your case also mean more tags and matching etc.

In your player script you should make a function:

void AddLife() {
   `HealthControl.LIVES++;`
}

And add another script on your buff item:

void OnCollisionEnter(Collision collision) {
  `if(collision.gameObject.Tag == "Player") {`
    `collision.gameObject.SendMessage("AddLife")`
    `Destroy(gameObject)`
  `}`
}

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 Tijs · Dec 29, 2012 at 05:53 PM 0
Share

I see, is there a way i can convert this to JS?

avatar image Lovrenc · Dec 29, 2012 at 05:57 PM 1
Share

All you have to do is:

function OnCollisionEnter(collision: Collision)

and
>`function AddLife()`

I think everything else stays the same.

avatar image
1

Answer by Tijs · Jan 04, 2013 at 07:28 PM

I got it like this, thnx:)

 var triggered: boolean;
 
 function OnTriggerEnter() {
 
     triggered = true;    
   }
 
 function Update() {
 
     if (triggered) { 
     Debug.Log("You have picked up a life");
     Destroy(gameObject);
     HealthControl.LIVES++;
       }
 
 } 


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

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

Trying to Destroy Object when Wave starts 1 Answer

PlayerPref or DontDestroyOnLoad for lives 3 Answers

how to save destroyed state of an object 1 Answer

Can't destroy an arrray with GUI Text 1 Answer

[CLOSED] Render priority of gameObjects 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