How to set a condition to pass the level?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Player_score : MonoBehaviour
{
private float timeLeft = 120;
public int PlayerScore = 0;
public GameObject TimeLeftUI;
public GameObject PlayerScoreUI;
//public Text scoreText;
//public bool EndLevel = false;
void Update ()
{
timeLeft -= Time.deltaTime;
TimeLeftUI.gameObject.GetComponent<Text>().text = ("Time Left: " + (int)timeLeft);
PlayerScoreUI.gameObject.GetComponent<Text>().text = ("Score: " + PlayerScore);
if (timeLeft < 0.1f)
{
SceneManager.LoadScene ("Juego1");
}
}
/*void LateUpdate ()
{
PlayerScore = int.Parse (scoreText.text);
if (PlayerScore >= 2)
{
EndLevel = true;
}
}*/
void OnTriggerEnter2D(Collider2D trig)
{
if (trig.gameObject.name == "explosivos")
{
PlayerScore += 1;
Destroy (trig.gameObject);
}
if (trig.gameObject.name == "EndLevel")
{
CountScore ();
}
}
void CountScore ()
{
PlayerScore = PlayerScore + (int) (timeLeft * 10);
}
}
`Hey, i want the player to be able to pass the level only if he has an X amount of points.
But points are only a UI element, and i am not sure if the script will be able to recognize them, also, i have the point manager in a different script.
To be clear, my game is a 2D, and i have a Trigger set as a goal.
If you know the answer, please tell me :D. (Better if it is in C#).
Answer by witylernn · Oct 20, 2017 at 02:26 AM
It is very hard to answer your question without seeing your code. But if you are saying the points are stored in a UI text element you could using something like this:
public Text scoreText; //assign the text element in the inspector
int score;
void Update()
{
score = Int.Parse(scoreText.text); //this converts the text of the UI element to a int variable.
if (score >= 20) // if the current score is = to or greater than (whatever number)
{
//do whatever you want.
}
}
Your answer
Follow this Question
Related Questions
Vertical Rays rapidly changing between "detecting ground" to "not detecting ground" 0 Answers
How can I do an Enemy while He's walking his HEAD always see the Target? 1 Answer
Error: An object reference is required to access non-static member, help? 2 Answers
2D Platformer Projectile wont face mouse position? 0 Answers