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 John Sartain · Mar 18, 2014 at 07:05 PM · particlesstringscorenoob

Score is added then immediately switched back

Score is added then immediately switched back. Its getting a little old cause im a noob and dont know javascipt and I didnt work this hard for no reason. These are my two scripts:

Ball:

 private var score : int = 0;
 var guiScore : GUIText;
 //var mainGameScript : MainGame;
 var particles_sparks : GameObject;
 var particles_squares : GameObject;
 
 //might not be needed sets score to 0 at beginning
 function Start (){
     guiScore.text = "Score: 0";
 }
 
 //add movement, call increase speed over time code,use contiuines update code
 function Awake(){
     rigidbody.AddForce(8, 8, 0, ForceMode.Impulse);
     InvokeRepeating("IncreaseBallVelocity", 2, 2);
     InvokeRepeating("UpdateScore", 0.05, 0.05);
 }
 
 //increase score by 1 each time
 function UpdateScore(){
     score += 1;
     guiScore.text = "Score: " + score.ToString();
 }
 
 //increase speed
 function IncreaseBallVelocity(){
     rigidbody.velocity *= 1.05;
 }
 
 //if ball below paddle load GameOver code
 function Update(){
     if(transform.position.y < -25) {
         GameOver();
     }
 }
 //if it hit sound, sparks|if hit brick destroy add score
 function OnCollisionEnter(col : Collision){
     audio.Play();
     Instantiate(particles_sparks, transform.position, transform.rotation);
         if(col.collider.name == "Brick"){
             Destroy(col.gameObject);
             score += 100;
             guiScore.text = "Score: " + score;
     }
 }
 
 //save score if above and load Menu
 function GameOver(){
     if(score > PlayerPrefs.GetInt("brickScore")){
         PlayerPrefs.SetInt("brickScore", score);
     }
     Application.LoadLevel("Menu");

[hide preview] 2045 characters / 248 words Can others in the community edit this? Score is added then immediately switched back. Its getting a little old cause im a noob and dont know javascipt and I didnt }

bonus Detect:

 var guiScore : GUIText;
 var particles_squares : GameObject;
 private var score : int = 0;
 
 //move to new script more than likely
 function OnTriggerEnter (col : Collider){
     if(col.transform.name == "Ball"){
         score += 5000;
         guiScore.text = "Score: " + score.ToString();
         guiScore.text = "Score: " + score;
         Instantiate(particles_squares, transform.position, transform.rotation);
         audio.Play();
     }
 }
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 Bovine · Mar 18, 2014 at 08:34 PM 0
Share

Not sure what's going on here, but seems you have two int values that store your score, you should have one and so the score value from you first script will keep changing the GUIText and so will the score value in your 2nd script - they are both updating the GUIText.

In C# I'd create a Score script, or Player$$anonymous$$etaData maybe and I'd have a getter setter for score that would fire an event, I'd subscribe to it and change the GUIText each time the score changed.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bash Mills · Mar 18, 2014 at 08:54 PM

The problem is you have two different variables keeping track of the same score. The values in each will actually be different. What you need is to keep it as one variable and simply add a score to that variable when you want to grant a bonus score. You can do this using a function. In the Ball script add this function:

 function AddScore(add : int){
     score += add;
 }

And then where ever the OnTriggerEnter function is take out all references to guiScore and score as you no longer need them. And then you need a reference to the ball script to call the function above. So change the code to this:

 var ballScript : Ball;
 var particles_squares : GameObject;
  
 function OnTriggerEnter (col : Collider){
     if(col.transform.name == "Ball"){
        ballScript.AddScore(5000);
        Instantiate(particles_squares, transform.position, transform.rotation);
        audio.Play();
     }
 }

Just don't forget to drag the ball object onto the variable in inspector. This should update the score as you want.

Apologies if my Javascript is a bit dodgy. I'm a C# man lol.

Comment
Add comment · Show 1 · 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 John Sartain · Mar 18, 2014 at 09:11 PM 1
Share

Dude thanks its worked ive tried for so long (cause im a noob and dont know javascript or c#) but thankfuly it worked! I was ready to give up. I need to learn javascript and c# sometime.

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

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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How To Stop Destroyer From Destroying? 2 Answers

Why do I have to call ToString() when fetching a String from another script? 2 Answers

Help with the update function and rigidbodys 3 Answers

Trying to get score counter... 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