Search Unity

Super Noob Here

Discussion in 'Scripting' started by hasnain9211, Jan 20, 2019.

  1. hasnain9211

    hasnain9211

    Joined:
    Jun 1, 2017
    Posts:
    2
    hi guys,

    I am new to dev and trying to figure out a code. Well i am storing game score in UIText name TotalScore. So what i want is to check score when timer ends and active or deactivate specific game object after checking score.

    Text CurrentScore i am passing TotalScore to it. But how to compare Text to Int? Please help.



    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

    public class MySCMnager : MonoBehaviour
    {
    void Awake()
    {
    PlayerPrefs.SetInt ("TotalScores", (0));
    }

    public float StageTimer;

    public GameObject Winner;
    public GameObject Looser;
    public int TargetScore;
    public Text CurrentScore;


    void Start()
    {
    Winner.gameObject.SetActive (false);
    Looser.gameObject.SetActive (false);
    //StageTimer = 5.0f;

    }

    void Update()
    {
    StageTimer -= Time.deltaTime;

    if (StageTimer <= 0.0f)
    {
    WinnerCheck ();
    }

    }

    void WinnerCheck()
    {
    if (CurrentScore >= TargetScore) {
    Winner.gameObject.SetActive (true);
    } else {
    Looser.gameObject.SetActive (true);
    }
    }


    }


    }
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    hasnain9211 likes this.
  3. hasnain9211

    hasnain9211

    Joined:
    Jun 1, 2017
    Posts:
    2
    Awesome Man... Thanks... I will use tags next time...
     
  4. ibbybn

    ibbybn

    Joined:
    Jan 6, 2017
    Posts:
    193
    Don't ever save scores as strings. When you need to update a text field convert the score int to a string by using ToString()
    For example:
    scoreText.text = TotalScore.ToString();

    Also it's Loser not Looser ;)
     
    Last edited: Jan 21, 2019