- Home /
Please Help Me
I have the following Highscore script and I have tried several ways and I can not find a way to get any script to tell this score to add a point to this script.
Here is the Highscore Script. All I need to do is send points over to this script when astroids are destroyed.
using UnityEngine;
using System.Collections;
using System.Text;
using System.Security;
public class Highscore : MonoBehaviour
{
public string secretKey = "12345";
public string PostScoreUrl = "http://YouWebsite.com/.../postScore.php?";
public string GetHighscoreUrl = "http://YouWebsite.com/.../getHighscore.php";
public string name = "";
public int score;
private string WindowTitel = "";
public string Score = "";
public int maxNameLength = 10;
public int getLimitScore = 15;
public int score1 = 0;
private bool start = false;
public float timer = 60;
public string stringToEdit = "Your Name";
void Update ()
{
timer -= Time.deltaTime;
if (timer <= 0 && start == false)
{
StartCoroutine(PostScore(name,score));
name = stringToEdit;
score = score1;
start = true;
}
}
IEnumerator GetScore()
{
Score = "";
WindowTitel = "Loading";
WWWForm form = new WWWForm();
form.AddField("limit",getLimitScore);
WWW www = new WWW(GetHighscoreUrl,form);
yield return www;
if(www.text == "")
{
print("There was an error getting the high score: " + www.error);
WindowTitel = "There was an error getting the high score";
}
else
{
WindowTitel = "Done";
Score = www.text;
}
}
IEnumerator PostScore(string name, int score)
{
string _name = name;
int _score = score1;
string hash = Md5Sum(_name + _score + secretKey).ToLower();
WWWForm form = new WWWForm();
form.AddField("name",_name);
form.AddField("score",_score);
form.AddField("hash",hash);
WWW www = new WWW(PostScoreUrl,form);
WindowTitel = "Wait";
yield return www;
if(www.text == "done")
{
Application.LoadLevel("HighScore");
}
else
{
print("There was an error posting the high score: " + www.error);
WindowTitel = "There was an error posting the high score";
}
}
void OnGUI()
{
GUI.Label(new Rect (10, 10, 1000, 20), "Score: " + score1);
stringToEdit = GUI.TextField(new Rect(Screen.width/2, Screen.height/2, 200, 50), stringToEdit, 25);
name = stringToEdit;
}
public string Md5Sum(string input)
{
System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
byte[] hash = md5.ComputeHash(inputBytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
sb.Append(hash[i].ToString("X2"));
}
return sb.ToString();
}
public void increaseScore(int _value)
{
score1 += 11;
}
}
Answer by Yokimato · Nov 22, 2012 at 07:58 PM
You have some functions as coroutines for keeping score that I'm not sure "fit" but since I have no idea what game you're going for I'll leave that one be. But, if you script is not working my suggestion would be to start small and work your way up.
On a general note, let's tackle how to tell the score controller something occurred. This can happen a couple ways:
You have static methods on the score controller in which case you can call ScoreController.UpdateScore(12) for example.
You have the instance of score controller on your calling script done a number of ways such as making it a public property and attaching via unity or finding it via GameObject.Find(), etc... and then calling it almost like option 1 (save it's no longer static)
You can take advantage of Unity's message bus and send it a message.
I would say option 2 and 3 would be the most "clean" ways of doing so. Here would be example code that would call you increaseScore method from another script:
//astroid script
void Destroyed() {
GameObject highScoreGameObject = GameObject.Find("GAME_OBJECT_NAME_HERE");
Highscore highScore = highScoreGameObject.GetComponent<Highscore>();
highScore.increaseScore(10);
}
This should make sense what's going on and is called from your asteroid script. However, architecture-wise, I would suggest making a controller that tracks all the "enemy" objects (like asteroids, enemy ships, etc) and when one dies have the tracking controller update the score so that it has a single point of update. I think it will be much easier to manage as you game becomes more complex.
Hope this helps!
YES THAN$$anonymous$$S SO $$anonymous$$UCH FINAL FIXED IT. YOU ROC$$anonymous$$ THAN$$anonymous$$S SO $$anonymous$$UCH.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Show score when enemy is destroyed C# 1 Answer
C# script to display score 0 Answers
Carrying Score over to another scene 1 Answer