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 davidflynn2 · Nov 22, 2012 at 07:33 PM · c#scorepoints

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;
 }
 
 
 
      
 }
 

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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:

  1. You have static methods on the score controller in which case you can call ScoreController.UpdateScore(12) for example.

  2. 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)

  3. 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!

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 davidflynn2 · Nov 22, 2012 at 09:04 PM 0
Share

YES THAN$$anonymous$$S SO $$anonymous$$UCH FINAL FIXED IT. YOU ROC$$anonymous$$ THAN$$anonymous$$S SO $$anonymous$$UCH.

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

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


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