- Home /
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();
}
}
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.
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.
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
Follow this Question
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