Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 nowerkie · Jan 26, 2017 at 09:20 AM · scripting problembeginner

How to put score and what codes to use

Hi im a beginner in unity and would love to have some help, I cannot see any tutorial regarding my game and i have no idea how to do this script and this is the last thing that i need. I only need the script for 1 scene and i have finished the other and i need this for my thesis.

Ok so here it is, I need an image that is moving from left to right using

  using UnityEngine;
  using System.Collections;
  
  public class BackAndForth : MonoBehaviour {
  
      public float delta = 100.0f;  // Amount to move left and right from the start point
      public float speed = 2.0f; 
      private Vector3 startPos;
  
      void Start () {
          startPos = transform.position;
      }
      
      void Update () {
          Vector3 v = startPos;
          v.x += delta * Mathf.Sin (Time.time * speed);
          transform.position = v;
      }
  }

My game is like a dart game that you need to click or shoot the target, the farther away the less score. But the difference is i'm using a princess as the target and the dart will be a kiss. Clicking will trigger the kiss and the target should be the lips. And also i don't know how to put score in that but i have a scoring system from the other scene. Here it is: I got this from one of the tutorials from internet

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class ScoreManager1 : MonoBehaviour {
 
     public Text scoreText;
     public Text hiScoreText;
 
     public float scoreCount;
     public float hiScoreCount;
 
     public float pointsPerSecond;
 
     public bool scoreIncreasing;
 
 
 
     // Use this for initialization
     void Start () {
     
 
     }
     
     // Update is called once per frame
     void Update () {
 
         if (scoreIncreasing) {
             scoreCount -= pointsPerSecond * Time.deltaTime;
         }
         if (scoreCount > hiScoreCount) {
             hiScoreCount = scoreCount;
         }
 
 
         scoreText.text = "Score: " + Mathf.Round (scoreCount);
         hiScoreText.text = "High Score: " + Mathf.Round (hiScoreCount);
     }
 
 }

I hope you guys can help me with this. Would really appreciate your help i've been stuck here for 1 month :(

Comment
Add comment · Show 2
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 malkere · Jan 26, 2017 at 10:27 AM 0
Share

https://unity3d.com/learn/tutorials

You don't really have anything going on here yet. The first script is only moving the transform to the right. The second script is subtracting from scoreCount every frame and updating the scoreText and hiScoreText. Have you assigned these to something in the editor? Your whole kiss mechanism isn't shown anywhere here so I have to ignore that. For a high score it sounds more like you want to be calculating the distance than the time. Have you gone through ALL of these?

https://unity3d.com/learn/tutorials

You should go through ALL of them several times until you know what's going on if you have any interest in making games. Doesn't take a month.

avatar image nowerkie malkere · Jan 27, 2017 at 04:43 AM 0
Share

Ok i figured it out a little. But my problem now is the High Score system. It restarts everytime i finished the game or the game ends the High Score is not saving i already tried setfloat and get float but i dont know what im missing here are the scripts. using UnityEngine; using System.Collections; using UnityEngine.UI;

 public class Score$$anonymous$$anager1 : $$anonymous$$onoBehaviour {
     
     public Text scoreText;
     public Text hiScoreText;
     public GameObject plus$$anonymous$$enuHolder;
     public GameObject $$anonymous$$$$anonymous$$enuHolder;
 
     public float scoreCount;
     public float hiScoreCount;
 
     public float pointsPerSecond;
 
     public bool scoreIncreasing;
 
 
 
     // Use this for initialization
     void Start () {
     
 
         if (PlayerPrefs.Has$$anonymous$$ey ("HighScore")) {
             hiScoreCount = PlayerPrefs.GetFloat ("HighScore");
 
         }
 
 
     }
     
     // Update is called once per frame
     void Update () {
 
         if (scoreIncreasing) {
             scoreCount -= pointsPerSecond * Time.deltaTime;
             PlayerPrefs.SetFloat("Score", scoreCount);
         }
 
         if (scoreCount > hiScoreCount) 
         {
             hiScoreCount = scoreCount;
             PlayerPrefs.SetFloat ("HighScore", hiScoreCount);
         }
 
         scoreText.text = "Score: " + $$anonymous$$athf.Round (scoreCount);
         hiScoreText.text = "High Score: " + $$anonymous$$athf.Round (hiScoreCount);
     }
 
 }

here is for my score shadowing for another scene:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class ScoreShadow : $$anonymous$$onoBehaviour
 {
     public Text scoreText;
     public Text hiScoreText;
     public float scoreCount = PlayerPrefs.GetFloat ("Score");
     public int hiScoreCount = PlayerPrefs.GetFloat ("HighScore");
     public bool alive;
 
     void Start (){
         alive = true;
     }
 
 
     public void Lips(){
         scoreCount += 100;
         
     }
     public void Cheeks(){
         scoreCount += 50;
         
     }
     public void Nose(){
         scoreCount += 20;
         
     }
     public void ForeHead(){
         scoreCount += 10;
         
     }
     public void $$anonymous$$(){
         
         scoreCount -= 50;
         
     }
     void Update ()
     {
 
 
         scoreText.text = "Score: " + $$anonymous$$athf.Round (PlayerPrefs.GetFloat ("Score")+scoreCount);
         hiScoreText.text = "High Score: " + $$anonymous$$athf.Round (hiScoreCount);
 
         if ((scoreCount+PlayerPrefs.GetFloat ("Score")) > hiScoreCount) {
             hiScoreCount = PlayerPrefs.GetFloat ("Score")+scoreCount;
             PlayerPrefs.SetInt("HighScores", hiScoreCount);
         }
                                                          
     }
 
 }
 

I've beem trying to figure it out but i cant see what is wrong.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by goutham12 · Jan 27, 2017 at 09:14 AM

first delete all your player preference using playerpreferences.deleteall()....

after check your scripts is any scrite having PlayerPrefs.SetInt("HighScores",0) in start or awake methods. may this help to you......

you are running your highscore updation code in update try to avoid that thing.

Comment
Add comment · 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
0

Answer by malkere · Jan 27, 2017 at 10:05 AM

PlayerPrefs is used for storing persistent data, like a high score, but not a score which is temporary. The difference being a score only gets used -while playing the game- the highscore gets saved so it can be looked at the next day or whenever. So you don't need to use

 PlayerPrefs.GetInt("Score");

just keep that as a variable in your script, the

 int score;

Now one thing you need to understand is why you can't do this:

 public float scoreCount = PlayerPrefs.GetFloat ("Score");

You're declaring a variable when you call public float like that, and it's called initializing when you set it equal to something. That's all you're allowed to do at that point, before you get into any methods. After that you have your Start() method, inside of which you could call PlayerPrefs.GetFloat("Score"); because that is a part of the script that gets executed, or run. The scoreCount is just a variable and only gets used because it's not in a method, so you can't called execution like GetFloat there.

How about something like this: using UnityEngine; using System.Collections; using UnityEngine.UI;

 public class MyClass : MonoBehaviour {
 
     float score = 0;
     Text scoreDisplay;
     Text highScoreDisplay;
     //you cannot call PlayerPrefs or any method here
     float currentHighScore;
 
     //declare initial values here
     void Start () {
         scoreDisplay = GameObject.Find("MainUI/ScoreDisplay").GetComponent<Text>();
         highScoreDisplay = GameObject.Find("MainUI/HighScoreDisplay").GetComponent<Text>();
         //call PlayerPrefs inside a method like Start();
         currentHighScore = PlayerPrefs.GetFloat("HighScore");
         highScoreDisplay.text = Mathf.RoundToInt(currentHighScore).ToString();
     }
 
     void AddToScore (int amount) {
         score += amount;
         //Call another method to do something for you
         UpdateScore();
     }
 
     void UpdateScore () {
         scoreDisplay.text = Mathf.RoundToInt(score).ToString();
 
         //don't update the highscore when you don't need to
         if (score > currentHighScore) {
             PlayerPrefs.SetFloat("HighScore", score);
             //try to call outside methods as little as possible by storing and reusing
             currentHighScore = score;
             highScoreDisplay.text = Mathf.RoundToInt(currentHighScore).ToString();
         }
     }
     
 }

There are tons of different ways to do anything you can imagine. This is how I might get started writing something along these lines.

Comment
Add comment · 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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to set y rotation axis to 90 on 2d project, always, forever 1 Answer

does SetResolution() change the resolution in editor game screen??? 1 Answer

How to use a timer with a button 2 Answers

True and false work weird? 1 Answer

How would I make a ListChangedEvent? 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