Hello, First of all sorry for the title, I really couldn't came up with a good title xD I have made a point system, included with GUI.Label. I don't like the look of those numbers in game. So I want to make it that if I have ''1'' point, there will come up a sprite with the number 1, which looks better. I know how to make it because I already made it with my countdown system aswell. In my countdown system it looked like this: Code (CSharp): float timeRemaining = 4.0f; void Update () { timeRemaining -= Time.deltaTime; } void OnGUI(){ if (timeRemaining > 2 && timeRemaining < 3) count3.SetActive (true); else count3.SetActive(false); if(timeRemaining > 0 && timeRemaining < 1) count1.SetActive(true); else count1.SetActive(false); if(timeRemaining > 1 && timeRemaining < 2) count2.SetActive(true); else count2.SetActive(false); if(timeRemaining > 0){ GUI.Label(new Rect(600, 600, 1000, 600), "Time Remaining : "+timeRemaining); } else{ GUI.Label(new Rect(600, 600, 1000, 600), "Time's Up"); Application.LoadLevel ("InGame"); } } } (count1,2,3 are sprites) But if I have a point system, the player can reach points like 27, 99, 375, 214, 69 and more... Its isnt really nice to write that all out and use tons of sprites (for every number). Instead of that I want to only have the numbers 0,1,2,3,4,5,6,7,8,9 and if I reach number 12, as example, there will come number 12 automaticly (with the sprites 1 and 2). And if I have number 235... yeah you understand it... Is this possible, or is there a better way to do this? Thank you.
You can try this: Code (CSharp): public class NewBehaviourScript : MonoBehaviour { public float timeRemaining = 106.0f; public int third = 0; public int second = 0; public int first = 0; public GameObject[] FirstSprites = new GameObject[10]; public GameObject[] SecondSprites = new GameObject[10]; public GameObject[] ThirdSprites = new GameObject[10]; void Start() { ActivateSprites(timeRemaining); } void Update() { timeRemaining -= Time.deltaTime; ActivateSprites(timeRemaining); } void ActivateSprites(float time) { // for example 159 // tempValue = 9 int tempValue = (int)time % 10; if (first != tempValue) { // FirstSprites[first].SetActive(false); // FirstSprites[tempValue].SetActive(true); Debug.Log("First. Deactivate : " + first); Debug.Log("First. Activate : " + tempValue); first = tempValue; } // tempValue = 5 tempValue = (int)time / 10 % 10; if (second != tempValue) { // SecondSprites[second].SetActive(false); // SecondSprites[tempValue].SetActive(true); Debug.Log("Second. Deactivate : " + second); Debug.Log("Second. Activate : " + tempValue); second = tempValue; } // tempValue = 1 tempValue = (int)time / 100; if (third != tempValue) { // ThirdSprites[third].SetActive(false); // ThirdSprites[tempValue].SetActive(true); Debug.Log("Third. Deactivate : " + third); Debug.Log("Third. Activate : " + tempValue); third = tempValue; } } }
I just a basic idea and u can optimize this, for example: Code (CSharp): // Sprites from assets public Sprite[] NumbersSprites = new Sprite[10]; // Or SpriteRenderer public Image FirstNumber; public Image SecondNumber; public Image ThirdNumber; private void Update() { timeRemaining -= Time.deltaTime; FirstNumber.sprite = NumbersSprites[(int)timeRemaining % 10]; SecondNumber.sprite = NumbersSprites[(int)timeRemaining / 10 % 10]; ThirdNumber.sprite = NumbersSprites[(int)timeRemaining / 100]; }
So the 2 messages you send are 2 different scripts or....?? Unity doesn't know ''Image''. What does this do? public Image FirstNumber; public Image SecondNumber; public Image ThirdNumber;
You need add "using UnityEngine.UI;". Image - https://unity3d.com/ru/learn/tutorials/modules/beginner/ui/ui-image FirstNumber and other is you UI sprites: https://getsharex.com/
Thankyou, it works perfectly One question: Is there a way to hide the ''0's'' when they aren't used. So if I have the letter 5; I don't see 005, but just only 5. Thanks.
You need validate current numbers. For example(i dont test this in unity, just typed here): Code (CSharp): private void Update() { timeRemaining -= Time.deltaTime; int tempValue = (int)timeRemaining / 100; if (tempValue == 0) ThirdNumber.sprite = NumbersSprites[tempValue]; else // Hide number ThirdNumber.sprite = null; int tempValue_2 = (int)timeRemaining / 10 % 10; if (tempValue != 0 && tempValue_2 != 0) SecondNumber.sprite = NumbersSprites[tempValue_2]; else // Hide number SecondNumber.sprite = null; tempValue = (int)timeRemaining % 10; FirstNumber.sprite = NumbersSprites[tempValue]; }
My code now: Code (CSharp): using UnityEngine; using System.Collections; using UnityEngine.UI; public class PointSystem : MonoBehaviour { public float pointText; public Sprite[] NumbersSprites = new Sprite[10]; public Image FirstNumber; public Image SecondNumber; public Image ThirdNumber; void Start () { pointText = 0f; } private void Update () { FirstNumber.sprite = NumbersSprites[(int)pointText % 10]; SecondNumber.sprite = NumbersSprites[(int)pointText / 10 % 10]; ThirdNumber.sprite = NumbersSprites[(int)pointText / 100]; int tempValue = (int)pointText / 100; if (tempValue == 0) ThirdNumber.sprite = NumbersSprites[tempValue]; else // Hide number ThirdNumber.sprite = null; int tempValue_2 = (int)pointText / 10 % 10; if (tempValue != 0 && tempValue_2 != 0) SecondNumber.sprite = NumbersSprites[tempValue_2]; else // Hide number SecondNumber.sprite = null; tempValue = (int)pointText % 10; FirstNumber.sprite = NumbersSprites[tempValue]; } void OnTriggerEnter2D (Collider2D other) { if (other.gameObject.tag == "Player") { pointText += 1; Debug.Log ("The score should update +1"); } } void OnGUI () { GUI.Label(new Rect(-100, 10, 600, 900), "Score : "+pointText); } } The code you send disables the source image but than it doesn't disable the color, so the whole image has to be disabled. http://i.imgur.com/PNRGylL.gifv btw. I use the same software as you use to make a gif, but how do I paste it into a thread like you?
So Image.sprite = null does work. You need then try this: Code (CSharp): ThirdNumber.enable = false; //or ThirdNumber.gameObject.SetActive(false); Also you need extra check: Code (CSharp): int tempValue = (int)pointText / 100; if (tempValue == 0) { if (!ThirdNumber.enable) ThirdNumber.enable = true; ThirdNumber.sprite = NumbersSprites[tempValue]; } else // Hide number ThirdNumber.enable = false; To past gif use "Upload a File".
only the second number doesn't appear, but it also doesn't appear if I reach 10+. Code (CSharp): public class PointSystem : MonoBehaviour { public float pointText; public Sprite[] NumbersSprites = new Sprite[10]; public Image FirstNumber; public Image SecondNumber; public Image ThirdNumber; void Start () { pointText = 0f; } private void Update () { FirstNumber.sprite = NumbersSprites[(int)pointText % 10]; SecondNumber.sprite = NumbersSprites[(int)pointText / 10 % 10]; ThirdNumber.sprite = NumbersSprites[(int)pointText / 100]; int tempValue = (int)pointText / 100; if (tempValue == 0) ThirdNumber.sprite = NumbersSprites[tempValue]; else // Hide number ThirdNumber.gameObject.SetActive(false); int tempValue_2 = (int)pointText / 10 % 10; if (tempValue != 0 && tempValue_2 != 0) SecondNumber.sprite = NumbersSprites[tempValue_2]; else // Hide number SecondNumber.gameObject.SetActive(false); tempValue = (int)pointText % 10; FirstNumber.sprite = NumbersSprites[tempValue]; } void OnTriggerEnter2D (Collider2D other) { if (other.gameObject.tag == "Player") { pointText += 1; Debug.Log ("The score should update +1"); } } void OnGUI () { GUI.Label(new Rect(-100, 10, 600, 900), "Score : "+pointText); } } I also can't use ''enable'' (also in the extra check there is ''enable'') click^^
You are deactivate gameobject, and never activate it back. I have already said that you need additional check.
Sorry but everything is new to me. I don't really understand what you mean with ''First get gameObject from Image''. Please explain it to me
Try to check this - http://unity3d.com/learn/tutorials/topics/scripting For example for activating gameobjects - http://unity3d.com/learn/tutorials/modules/beginner/scripting/activating-gameobjects?playlist=17117 You have a Image(for example ThirdNumber) - http://docs.unity3d.com/ScriptReference/UI.Image.html In docs you see that Image has gameObject variable, you can get it - ThirdNumber.gameObject Then you check if this gameobject is active - ThirdNumber.gameObject.activeSelf
Code (CSharp): int tempValue = (int)pointText / 100; if (tempValue == 0) { if (ThirdNumber.gameObject.SetActive(false)) ThirdNumber.gameObject.SetActive(true); ThirdNumber.sprite = NumbersSprites[tempValue]; } else // Hide number ThirdNumber.gameObject.SetActive(false); I did this but I got this error: