Sign up Calendar Latest Topics
 
 
 


Reply
  Author   Comment  
J53 Industries

Senior Member
Registered:
Posts: 107
Reply with quote  #1 
Hello,
I have scripting question regarding a score system. My question is how would I add one point to the score when a gameobject is destroyed. Right now when I destroy a gameobject the Score goes to 1 however it stays there. What I am trying to do is make the score increase by one point each time a gameobject is destroyed. I will post my script below. Thanks in advance


var Zombie : GameObject;
var ScoreText : UI.Text;
private var Score : int = 0;
function OnMouseDown () {
if (Input.GetMouseButtonDown(0))
{
Destroy (Zombie);
UpdateScore();
 } 
}
function UpdateScore() {
Score += 1;
ScoreText.text = "Score: " + Score.ToString();

function OnCollisionEnter2D (collision : Collision2D) 
{
if (collision.gameObject.tag == "Zombie")
{
GameOver();
}
}
function GameOver()
{
  if(Score > PlayerPrefs.GetInt("Score")) 
  {
      PlayerPrefs.SetInt("Score", Score);
      PlayerPrefs.Save();
    
    Debug.Log(PlayerPrefs.GetInt("Score").ToString());    
   
     }
    
    Application.LoadLevel("0");
}


__________________
Check Out My Apps Below

https://play.google.com/store/apps/developer?id=J53+Industries
0
Vaabenhus

Junior Member
Registered:
Posts: 21
Reply with quote  #2 
Hey J53, I think you can use the OnDestroy method here to accomplish this. Every monobehavior has this method, you just have to "override" it. In your script for the gameobjects being destroyed you can add something like:

function OnDestroy()
{
    PlayerPrefs.SetInt("Score", <insert the added score here>);
}


I'm not 100% sure on the javascript here, since I normally use C# but hopefully it will give you an idea. [smile]

__________________

River Dash (Google Play)
Trail Maze Challenge (Google Play)

0
mzr

Senior Member
Registered:
Posts: 222
Reply with quote  #3 
I assume you have this script attached to one object.
If that's the only script that you have regarding killing zombies then it will probably only work for that one zombie that you have assigned to the zombie variable, therefore it will destroy that zombie on mouse click and add 1 to score. But after that when you click your mouse the script will try to destroy the object that's under zombie variable, unfortunately you already destroyed it and therefore there's nothing to destroy now, so the script will probably show an error and stop working (before adding another point).

If that's the case you'd need to get a new target for zombie variable each time you click. You could use raycast for that. Check out this link for more info: http://answers.unity3d.com/questions/247810/how-to-get-gameobject-that-is-clicked-by-a-mouse.html

__________________

http://www.howtomakemobilegames.com/post/twin-stick-shooter-7140396

My second game: Google Play
My first game: Google Play  Amazon iOS

0
J53 Industries

Senior Member
Registered:
Posts: 107
Reply with quote  #4 
Quote:
Originally Posted by mzr
I assume you have this script attached to one object. If that's the only script that you have regarding killing zombies then it will probably only work for that one zombie that you have assigned to the zombie variable, therefore it will destroy that zombie on mouse click and add 1 to score. But after that when you click your mouse the script will try to destroy the object that's under zombie variable, unfortunately you already destroyed it and therefore there's nothing to destroy now, so the script will probably show an error and stop working (before adding another point). If that's the case you'd need to get a new target for zombie variable each time you click. You could use raycast for that. Check out this link for more info: http://answers.unity3d.com/questions/247810/how-to-get-gameobject-that-is-clicked-by-a-mouse.html

I will try this thank you very much for your help
__________________
Check Out My Apps Below

https://play.google.com/store/apps/developer?id=J53+Industries
0
Martin

Avatar / Picture

Administrator
Registered:
Posts: 2,230
Reply with quote  #5 
Hey J53.
Also make sure that you update the score before you destroy the game object. I had a quick look at your code and it looks like you are destroying the zombie object before the score is updated. I don't know how your scripts are attached to your objects but just wanted to mention it incase thats the problems.

Its nice to still have some code knowledge in my head lol [biggrin]

Martin
0
Previous Topic | Next Topic
Print
Reply

Quick Navigation:

Easily create a Forum Website with Website Toolbox.