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 KarlH · May 01, 2011 at 07:39 PM · collisiondestroyscore

object destroy and scoring points

i've looked through questions already but i cant seems to find a specific answer/script to my problem. im making a ball rolling game which destroys objects in turn giving you a score. i have 2 scripts at the min:

static var score : float = 0;

function Update () { guiText.text = "Score : " + score;

}

this shows "score:" on the screen and my other script is:

function OnCollisionEnter ( collision : Collision){
    if (collision.gameObject.tag == "DestructibleObject")
    KeepScore.score++;

}

which is attatched to my player (rolling ball) and updates the "score:" by 1 everytime i hit an object named "DestructibleObject". the problem is that the score keeps increasing, im wanting to limit the score to 1 for each object. it would be even better if anyone can tell me how to destroy the object at the same time. i added the line:

Destroy (collision.gameObject);

so the script looks like:

function OnCollisionEnter ( collision : Collision){

 if (collision.gameObject.tag == "DestructibleObject")
 KeepScore.score++;
 Destroy (collision.gameObject);

}

but when i load the level the terrain is getting destroyed (i think when my ball thouches it to start with). if i remove the keepScore.score++; line i can go round the level fine (it doesn't get destroyed) and when i hit an object it is destroyed. when the 2 lines of script are together it just doesn't want to work : /

any help is highly appreciated

thanks.

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

1 Reply

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

Answer by GesterX · May 01, 2011 at 07:50 PM

I believe the problem is with your if statement syntax. It's always best to surround the code after the if with curly braces. So in your code:

var oneTime : boolean = false;

 function OnCollisionEnter ( collision : Collision){
     if(!oneTime){
     if (collision.gameObject.tag == "DestructibleObject"){
     KeepScore.score++;
     oneTime = true;
     }
 }
 }

Everything in the curly brackets gets called if the if-statement is satisfied

Comment
Add comment · Show 9 · 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 KarlH · May 01, 2011 at 08:01 PM 0
Share

omg! lol that worked perfectly i feel like such a muppet now. im still new to the scripting side of things, i wont be making that mistake again :D

thanks a bunch.

i'm still curious though, if the object isn't getting destroyed what script etc will i have to use to have a maximum of 1 point come from an object (ridgidbody) that still stays there in the scene?

avatar image GesterX · May 01, 2011 at 08:38 PM 0
Share

Could you reword that question? I didn't quite understand. Don't forget to mark this answer as the correct one and vote it up :)

avatar image KarlH · May 02, 2011 at 03:15 PM 0
Share

sorry if it didn't make any sense. right, ins$$anonymous$$d of the object getting destroyed when i touch it i'de like to know how to keep the object in the scene while reacting with physics (rigidbody) and recieve +1 score only. because at the $$anonymous$$ i can have the object in the scene but when im rolling into it the score will keep rising. basically i want to limit the score to +1 only.

thanks and hope that makes a bit more sense.

avatar image GesterX · May 02, 2011 at 03:28 PM 0
Share

I editted the code in the answer. Basically you have a variable called oneTime set to false. When you collide you check if oneTime is false (!oneTime means NOT oneTime). If it's false then the next part of the code is run. At the end of the code we set oneTime to true. This means the next time you collide oneTime will be true and the code after the if statement won't be run.

avatar image KarlH · May 02, 2011 at 09:24 PM 0
Share

this works fine but not how i'm wanting it to, it might of been me not explaining it right. since i have multiple objects in my scene tagged 'DestructibleObject' the score rises by 1 when i hit the first object but every other object i hit after that wont give out score. i realised what i should of said is to limit score given out by 1 from each individual object. what i might have to look into now is something like if the object has moved from original position then dont give any more score out.

thanks anyway, i appreciate all your help. il get my head into looking for different solutions :)

Show more comments

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

Scoring (i'm stuck) 3 Answers

Question about Point adding / Destroying after collision. 0 Answers

Score added on collision with another player 0 Answers

Is there another way of counting Scores (points) instead of Colliding (destroying) 1 Answer

destroy touch the screen on gameobject in camera 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