Parent fade in
Hey Guys,
Been making a simple timer that only activates while you're holding the main timer button down. And stops the timer once i let go of that button. At the same time after i let go, your 'personal best' appears at the bottom of the screen. So far i've just been using PersonalBestGroup.gameObject.SetActive(true); to make it appear once i've let go of that main timer button. Also making it 'false' to disappear when i reset or start the timer again.
Hoping to make that Personal Best Group that contains the actual text and reset button fade in nicely once i release the main button timer, rather than it appearing in out of nowhere.
Tried a couple tutorials, but think i'm getting confused on how to define the group... is it a 'CanvasGroup' or something else? or is my misunderstanding in actually how the gameObject.SetActive true/false makes things appear or not? I was worried about if i did make that group's alpha 0 to start with, that the reset button within it could still potentially be active. I don't know any better!
Also i should say everything is in my UI layer if that is effecting anything anything.
Any help would be much appreciated. Code of my whole timer (without my attempts at coding the fade) below if you need.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIScript : MonoBehaviour {
public Text m_counter;
public Text m_highscore;
string timerHours;
string timerMinutes;
string timerSeconds;
string timerSeconds100;
private float startTime;
private float stopTime;
private float timerTime;
public float m_timeButFloat;
string s_timerHours;
string s_timerMinutes;
string s_timerSeconds;
string s_timerSeconds100;
private bool isRunning = false;
public GameObject obj_PBStuff;
public GameObject btn_Start;
// Use this for initialization
void Start () {
if (PlayerPrefs.HasKey("Highscore") == true) {
m_timeButFloat = PlayerPrefs.GetFloat("Highscore");
DisplayHighScore();
}
else {
m_highscore.text = "00:00:00";
}
TimerReset();
}
public void TimerStart() {
if (!isRunning) {
isRunning = true;
startTime = Time.time;
obj_PBStuff.gameObject.SetActive(false);
}
}
public void TimerStop()
{
if (isRunning)
{
isRunning = false;
stopTime = timerTime;
m_timeButFloat = timerTime;
obj_PBStuff.gameObject.SetActive(true);
btn_Start.gameObject.SetActive(false);
}
if (PlayerPrefs.GetFloat("Highscore") < m_timeButFloat) {
SetHighscore();
}
}
public void SetHighscore ()
{
PlayerPrefs.SetFloat("Highscore", m_timeButFloat);
//m_highscore.text = PlayerPrefs.GetFloat("Highscore").ToString();
DisplayHighScore();
}
public void TimerReset()
{
stopTime = 0;
isRunning = false;
obj_PBStuff.gameObject.SetActive(false);
m_counter.text = "00:00:00";
btn_Start.gameObject.SetActive(true);
}
// Update is called once per frame
void Update () {
timerTime = stopTime + (Time.time - startTime);
int minutesInt = (int)timerTime / 60;
int hoursInt = (int)minutesInt / 60;
int secondsInt = (int)timerTime % 60;
int seconds100Int = (int)(Mathf.Floor((timerTime - (secondsInt + minutesInt * 60)) * 100));
if (isRunning)
{
timerHours = (hoursInt < 10) ? "0" + hoursInt : hoursInt.ToString();
timerMinutes = (minutesInt < 10) ? "0" + minutesInt : minutesInt.ToString();
timerSeconds = (secondsInt < 10) ? "0" + secondsInt : secondsInt.ToString();
timerSeconds100 = (seconds100Int < 10) ? "0" + seconds100Int : seconds100Int.ToString();
if (hoursInt > 0)
{
m_counter.text = timerHours + ":" + timerMinutes + ":" + timerSeconds + ":" + timerSeconds100;
}
else
{
m_counter.text = timerMinutes + ":" + timerSeconds + ":" + timerSeconds100;
}
}
}
public void DisplayHighScore(){
int s_minutesInt = (int)m_timeButFloat / 60;
int s_hoursInt = (int)s_minutesInt / 60;
int s_secondsInt = (int)m_timeButFloat % 60;
int s_seconds100Int = (int)(Mathf.Floor((m_timeButFloat - (s_secondsInt + s_minutesInt * 60)) * 100));
s_timerHours = (s_hoursInt < 10) ? "0" + s_hoursInt : s_hoursInt.ToString();
s_timerMinutes = (s_minutesInt < 10) ? "0" + s_minutesInt : s_minutesInt.ToString();
s_timerSeconds = (s_secondsInt < 10) ? "0" + s_secondsInt : s_secondsInt.ToString();
s_timerSeconds100 = (s_seconds100Int < 10) ? "0" + s_seconds100Int : s_seconds100Int.ToString();
if (s_hoursInt > 0)
{
m_highscore.text = s_timerHours + ":" + s_timerMinutes + ":" + s_timerSeconds + ":" + s_timerSeconds100;
}
else
{
m_highscore.text = s_timerMinutes + ":" + s_timerSeconds + ":" + s_timerSeconds100;
}
}
}
Your answer
Follow this Question
Related Questions
Child an Object to Another Objects Parent On Collision 1 Answer
How do I get the access of the parent component or root parent in Unity3d C# 1 Answer
reset a GameObject parents childs to 0 if a item has changed its parent 2 Answers
Parenting objects with code not working 2 Answers
Trying to set the parent of an instantiated object to another instantiated object 1 Answer