So I have a score and a timer in my game working completely fine but when the player reaches the end block I want a final score screen to appear which displays your score, number of collisions, time and what the target time for that level was however its always blank. I feel like I’m missing something really basic but my brain just refuses to come up with the answer.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreUI : MonoBehaviour
{
[SerializeField]
public static float score;
[SerializeField]
public static float timer;
[SerializeField]
public static float numberOfCollisions;
public float FinalScore;
public float targetTime;
public Text scoreText;
public Text timerText;
public Text FScoreText;
public Text FTimerText;
public Text collisionNumberText;
public Text FinalScoreText;
public Text TargetTimeText;
private EndBlock endblock;
public GameObject GameEndingBlock;
void Start ()
{
scoreText.GetComponent<Text> ();
timerText.GetComponent<Text> ();
FScoreText.GetComponent<Text> ();
FTimerText.GetComponent<Text> ();
collisionNumberText.GetComponent<Text> ();
TargetTimeText.GetComponent<Text> ();
endblock = GameEndingBlock.GetComponent<EndBlock> ();
}
// Update is called once per frame
void Update ()
{
timer += Time.deltaTime;
timer = Mathf.Round (timer * 100f) / 100f;
scoreText.text = score.ToString ();
timerText.text = timer.ToString ();
if (endblock.gameEnd == true)
{
FinalScore = (score + (targetTime - timer)) - numberOfCollisions;
FScoreText.text = score.ToString ();
FTimerText.text = timer.ToString ();
collisionNumberText.text = numberOfCollisions.ToString ();
FinalScoreText.text = FinalScore.ToString ();
}
}
}
This is the code on the player for when it hits the endblock it puts the endscreen on display which works but as mentioned its blank.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class EndBlock : MonoBehaviour
{
private string endblock = "endblock";
private GameObject finalScreen;
public bool gameEnd = false;
void Start()
{
finalScreen = GameObject.Find ("EndCardScreen");
finalScreen.gameObject.SetActive (false);
}
private void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == endblock)
{
finalScreen.gameObject.SetActive (true);
gameEnd = true;
Debug.Log ("You Win");
}
}
}
I assume it has something to do with the setActive as I put my code just into the update and it works perfectly fine so I’m not gonna lie I’ve gone slightly mad looking at this so any help is much appreciated.
Ok so after testing Ive found that it has to do with the prefab as I’ve moved around the code so the endblock is on the endblock now instead of the player and that it works when in scene but when it is instantiated from the level editor the final score screen doesnt display or work… i need some help cause im going slightly mad xD.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreUI : MonoBehaviour
{
[SerializeField]
public static float score;
[SerializeField]
public static float timer;
[SerializeField]
public static float numberOfCollisions;
public float FinalScore;
public float targetTime;
public Text scoreText;
public Text timerText;
public Text FScoreText;
public Text FTimerText;
public Text collisionNumberText;
public Text FinalScoreText;
public Text TargetTimeText;
private EndBlock endblock;
public GameObject GameEndingBlock;
private bool visitedFScreen;
void Start ()
{
scoreText.GetComponent<Text> ();
timerText.GetComponent<Text> ();
FScoreText.GetComponent<Text> ();
FTimerText.GetComponent<Text> ();
collisionNumberText.GetComponent<Text> ();
TargetTimeText.GetComponent<Text> ();
endblock = GameEndingBlock.GetComponent<EndBlock> ();
visitedFScreen = false;
}
// Update is called once per frame
void Update ()
{
timer += Time.deltaTime;
timer = Mathf.Round (timer * 100f) / 100f;
scoreText.text = score.ToString ();
timerText.text = timer.ToString ();
if (visitedFScreen == false)
{
if (endblock.gameEnd == true)
{
DisplayFinalScore ();
}
}
}
void DisplayFinalScore()
{
FinalScore = (score + (targetTime - timer)) - numberOfCollisions;
FScoreText.text = score.ToString ();
FTimerText.text = timer.ToString ();
TargetTimeText.text = targetTime.ToString ();
collisionNumberText.text = numberOfCollisions.ToString ();
FinalScoreText.text = FinalScore.ToString ();
visitedFScreen = true;
Debug.Log ("Target Time: " + targetTime);
Debug.Log ("FinalScore: " + FinalScore);
Debug.Log ("score: " + score);
Debug.Log ("Number Of Collision: " + numberOfCollisions);
Debug.Log ("Time Finished: " + timer);
}
}
Here is the endblock improved code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class EndBlock : MonoBehaviour
{
private string endblock = "player";
private GameObject fScreen;
public bool gameEnd = false;
void Start()
{
fScreen = GameObject.Find ("EndCardScreen") as GameObject;
fScreen.gameObject.SetActive (false);
Debug.Log("Found endcardscreen");
}
private void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == endblock)
{
fScreen.SetActive (true);
gameEnd = true;
Debug.Log("Displaying endcardscreen");
Debug.Log ("You Win");
}
}
}
I believe the main issue I’m having is that it tells me that isn’t set to a instance of a gameobject but the code works?
but yet the code is working fine from the piece that isnt a prefab so Im confused :L