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 user-12002 (google) · Apr 26, 2011 at 06:58 PM · triggerdestroyscore

Problem with a score script

Hi,

I'm creating a very basic 2D platformer. I have already the basic move functions working. But now i'm trying to make a score script. The idea is that if you hit a red square that you get 1 point. I added a box collider to it that is triggered and i already have the +1 point working, the only problem now is that when i hit the red square, every red square in the scene dissapaers. Every red square is tagged 'score'

This is a part of my move around script:

static var score : int = 0; static var AmtScoreHits : int = 0;

function OnTriggerEnter(hit : Collider) { if(hit.gameObject.tag == "score") { score = score+1; AmtScoreHits = AmtScoreHits +1; } }

And this is the script atached to the red square, the score object:

function Update() { if(MoveAround.AmtScoreHits == 1) { Destroy(gameObject);

} }

And sorry if they're a lot of spelling mistakes but i come from Belgium ;)

Comment
Add comment · Show 3
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 Muzz 1 · Apr 26, 2011 at 07:01 PM 1
Share

Just a point, you can use '++' which means +=1.

avatar image xCRKx TyPHooN · Apr 26, 2011 at 07:04 PM 1
Share

Or you can use += 1 ;)

avatar image user-12002 (google) · Apr 26, 2011 at 07:10 PM 0
Share

Ok thank you for the tip :D

1 Reply

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

Answer by xCRKx TyPHooN · Apr 26, 2011 at 07:16 PM

Try this...

if(hit.gameObject.tag == "score")
{
score++;
AmtScoreHits++;
Destroy(hit.gameObject);
}

This will destroy the gameObject that collides immediately when you pick it up. If you don't want to destroy it immediately, it looks like you are doing everything right EXCEPT this line...

if(MoveAround.AmtScoreHits == 1)

I'm guessing MoveAround is a script? The problem here is that the variable AmtScoreHits in MoveAround is 1 for everything, so every red square is getting that same script and triggering the destroy function.

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 Scribe · Apr 26, 2011 at 07:20 PM 2
Share

static var score : int = 0; static var AmtScoreHits : int = 0;

function OnTriggerEnter(hit : Collider){ if(hit.gameObject.tag == "score"){ score++; AmtScoreHits++; Destroy(hit.gameObject); } }

that should work now I have edited it. You only need one script and this is a replacement to the "part of my move script" script that goes on your character or whatever is hitting the scoring collider

hope that helps

Scribe

avatar image user-12002 (google) Scribe · Apr 26, 2011 at 07:30 PM 0
Share

I didn't triy you're solution because the answer before you worked already thanks anyway :)

avatar image Scribe Scribe · Apr 26, 2011 at 08:12 PM 0
Share

both are answers were the same except he was 20seconds faster :)

avatar image Scribe · Apr 26, 2011 at 07:21 PM 0
Share

got there just before me :P

avatar image user-12002 (google) · Apr 26, 2011 at 07:29 PM 0
Share

Ok thank you very much, this was just what i was looking for ;D

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

Adds too many points to the score? 0 Answers

how do i count leftover enemies in screen? 0 Answers

Scoring System 2 Answers

How do I call an on trigger enter / destroy gameObject in the scene c# 1 Answer

How to have it so if I push an object into a cirten object, the pushed object but not me will be destoryed? 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