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 TheDefibulator · Feb 27, 2013 at 10:37 PM · collisionscore

How do I make my score go up upon a collision?

As it stands right now, I have a cube and when it is hit by a ball shot by the player, it changes green and plays a noise. I would like to make a score system that goes up with correct answers and down with incorrect answers. Please help, I'm new to Unity! Thanks

 var LevelToLoad : String;
 public var CorrectSound : AudioClip;
 function OnCollisionEnter (Collision){
     gameObject.renderer.material.color = Color.green;
     audio.PlayOneShot(CorrectSound);
     yield WaitForSeconds (3);
     Application.LoadLevel(LevelToLoad);
 };
 //This code is associated with the collision box. If the ball hits it, it will play the designated sound, wait three seconds then advance to the next level.
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 hoy_smallfry · Feb 28, 2013 at 01:43 AM

First you need to create a variable that all scripts can be aware of.

With Javascript, you can accomplish this by marking a variable as both public and static. The public keyword makes it so that other scripts can access the variable, and the static keyword makes it so that you do not need to attach the script to a GameObject to use this variable.

 // in a script called "TestScriptA.js"
 public static var score : Number = 0  

Now, all another script needs to do to access this variable is to call it by the script name, use the . to show that the "TestScriptA.js" is the owner of the variable:

 // in a different script called "TestScriptB.js"
 function Update ()
 {
     // press 'A' down to add 1 to the score
     if(Input.GetKeyDown(KeyCode.A))
     {
         TestScriptA.score++;
     }

     // press 'S' down to subtract 1 from the score
     if(Input.GetKeyDown(KeyCode.S))
     {
         TestScriptA.score--;
     }
 }

Now, you need another function to show the score on the screen, if that's what you want to do. You can either put this in "TestScriptA.js", or in a separate script. But be aware that if you want Unity to call OnGUI(), the script does have to be attached to a GameObject:

 function OnGUI()
 {
     // define the size of the area that will have the text
     var rectangle : Rect = Rect(10, 10, 100, 100);
     
     // draws the text in the rectangle on the screen.
     GUI.Label(rectangle, "Score: " + TestScriptA.score);
 }

I hope that helps!

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 TheDefibulator · Feb 28, 2013 at 04:37 AM

Thanks, that helps a lot! What is the if statement for a collision? and also, how do I edit the size of the text? It is too small for the resolution

Comment
Add comment · Show 3 · 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 28, 2013 at 06:44 AM 0
Share

1) ins$$anonymous$$d of an posting a new answer, post a comment to an anser, like this.

2) what do you mean by "if statement for a collision"? What are you hoping to do?

3) For changing GUI styles, look at this or even this. $$anonymous$$eep in $$anonymous$$d that Unity Answers has tons of questions that have been asked like yours, so do some searching on here and on Google and I guarantee you you'll find the answer to beginner questions.

avatar image TheDefibulator · Feb 28, 2013 at 07:42 AM 0
Share

Thanks! I figured it out. And, I did some pretty extensive searching and most of the suggestions did not fit what I was looking for so I decided to post.

avatar image hoy_smallfry · Feb 28, 2013 at 06:43 PM 0
Share

You're new here, right? well usually, when you find an answer that's right for you, you should select the check mark under the vote up/down icons on the correct answer. Cheers!

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

How to add the right amount of points when my player destroys a gameobject? 0 Answers

Multiple scores from collisions how do I stop after one collsion 3 Answers

Questions about Is Triggers and player collision. HELP! 2 Answers

Score updating after collision 1 Answer

Why am I getting a negative score? 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