Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question help

Discussion in 'UGUI & TextMesh Pro' started by trevorlovesmelody1, Jun 8, 2023.

  1. trevorlovesmelody1

    trevorlovesmelody1

    Joined:
    Feb 19, 2022
    Posts:
    3
    i made i counter for when i pick up gems but it not wanting to change right instead it does this
    upload_2023-6-7_21-42-1.png
    plz help
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,202
    Share the code and font settings if you use a custom font
     
  3. trevorlovesmelody1

    trevorlovesmelody1

    Joined:
    Feb 19, 2022
    Posts:
    3
    this is the code


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using TMPro;

    public class PlayerMovement : MonoBehaviour
    {
    private float horizontal;
    public float speed = 8f;
    public float jumpingPower = 4f;
    private bool isFacingRight = true;
    private int pickUp;
    private int money;

    [SerializeField] public TextMeshProUGUI moneyCounter;
    [SerializeField] private Rigidbody2D rb;
    [SerializeField] private Transform groundCheck;
    [SerializeField] private LayerMask groundLayer;

    void Start()
    {
    money = 0;
    SetMoneyCounter();
    }

    void Update()
    {
    horizontal = Input.GetAxisRaw("Horizontal");

    if (Input.GetButtonDown("Jump") && IsGrounded())
    {
    rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
    }

    Flip();
    }

    void SetMoneyCounter()
    {
    moneyCounter.text = "Money: " + money.ToString();
    }

    private void FixedUpdate()
    {
    rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
    }

    private bool IsGrounded()
    {
    return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);
    }

    private void Flip()
    {
    if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f)
    {
    isFacingRight = !isFacingRight;
    Vector3 localScale = transform.localScale;
    localScale.x *= -1f;
    transform.localScale = localScale;
    }
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
    if (other.gameObject.CompareTag("PickUp"))
    {
    other.gameObject.SetActive(false);
    money++;
    SetMoneyCounter();
    }
    }
    }