I try to learn C# and Unity, I am beginner
I`m working to a little game for me and i want to do a high score but i really don t know how
I did the score and it work, not how i want, but it work
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Score : MonoBehaviour
{
public Transform player;
public Text scoreText, highScoreText;
public int score, highScore;
// Update is called once per frame
void Update()
{
scoreText.text = player.position.z.ToString("0");
}
public void UpdateHighScore(){
if(score > highScore){
highScore = score;
highScoreText.text = highScore.ToString();
PlayerPrefs.SetInt("HighScore", highScore);
}
}
}
that is my script.
and the other problem i have is…
for example when i finish the first level i have 500score, and when i continue to level 2 my score is 0
what i have to do to continue in level 2 with 500score for example? i tryed to do it by myself but is not working, how i said im a really beginner Is a game like Brackeys tutorials(i guess many of you know that videos). But i try to put in this game a lot of things, to see how it works everything, score, menu,respawn, ads etc...isnt a real game, is just a experiment
You can make your variable static:
This will make it accessible across different scripts. I used it here to keep count of enemies so I knew when to load the next scene. Hope this helps!
-Z
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class destroy : MonoBehaviour
{ public static int opCount = 3; //to access from another script, do FILENAME.Varname
// so for me it was destory.opCount