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 03, 2011 at 10:24 PM · collisionpickuphealth

'Deadly' area disappears on collision.

I have an area on my terrain which if hit, you die/loose health. Only problem is, when you do it disappears. The problem comes from the fact i have an on collision pick up script for my 'coins' which when you collide with them, they disappear.

My pick up code is as follows -

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

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 >= 5) { Destroy(wall); } }

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

The code i have on my hot spot is -

var spawn : Transform;

function OnTriggerEnter(other : Collider) { if (other.tag == "Player") { other.transform.position = spawn.position; } }

I've confused myself trying to read through that and figure out what to do. Any help would be greatly appreciated.

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

Answer by GesterX · May 03, 2011 at 11:41 PM

The reason it disappears is because you have this:

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); }

Your code is doing this...

  1. You hit a danger area
  2. The code checks whether the danger area is tagged as coin
  3. It is not so it goes to the "else" part of the code
  4. In your else code you are saying add 5 to the score, update the GUI, print to the debug log, and then destroy the object the collider is attached to (i.e. the danger area)

You need to either tag your danger areas and do a check in your code for the tag or you could create a new script all together for danger areas (if they are going to have a lot of functionality that other objects won't)

EDIT - I would remove the destroy call from the else part of the statement all together

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
avatar image
0

Answer by Owen-Reynolds · May 03, 2011 at 11:33 PM

That first script is on the player, right? Right now, hitting a coin is worth 1, but, for no good reason I can see, hitting a wall, tree, rock ... is worth 5. If you got rid of the else, the code would say to do nothing if you hit a non-coin.

If you want coins to have different values, you could make a "bigCoin" tag and also check for that. Or you could give coins a tiny script with just var coinVal = 1; (change in inspector.) The player would then add that coin's value.

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

No one has followed this question yet.

Related Questions

Add Health on Pickup to Decaying Health,Make a collision with an object add health 2 Answers

For loop on collision (two hit kill) 2 Answers

Leap motion: how to pickup a custom mesh 0 Answers

In-game pickup not moving towards player after being collected 1 Answer

How to add health pickup/potion 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