Ok so i asked a question yesterday regarding why my final score wasn’t displaying correctly but well I’ve now got a weird error to do with it. basically I’m using a c# level editor to create my levels and the endpoint when spawned as a prefab just doesn’t like the code I’ve used. My code works fine when I have the object in the scene hierarchy but this is the current error I’m now getting and I would love some help. thank you very much for any help. all code and picture of error below:
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.gameObject.SetActive (true);
gameEnd = true;
Debug.Log("Displaying endcardscreen");
Debug.Log ("You Win");
}
}
}
I’m not understanding what you’re asking…is something wrong with your level code? with spawning prefabs? with the score? what object in the scene hierarchy?
you also said something about spawning but there’s non of that in the code you posted…could you please try to rephrase your question and attach all relevant code.
Ok ill explain in more detail sorry for any confusion. This whole code is designed that when the player hits the gameobject called endblock it sends a bool value to display the endcardscreen. Now from I’ve noticed it finds the endcardscreen fine and I’ve debugged it and it displays the panel called endcardscreen fine however it either appears blank or just doesn’t display sometimes. I’m missing something really basic and its annoying. I either get a null reference exception or the code works fine but just displays an empty screen like shown below. The worst part was that it was working and so heres all the code to do with this error. If you need more screenshots like me know cause limited to 2 screenshots per post
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);
}
}
and legit i just reopened to grab the code and screenshots and the error is gone but it appears and disappears but all my debugging shows that a) it finds the endcardscreen b) displays with zero info or c) i get an error like shown above. @nobx101 @DanielleSisserman @JVLVince @Hellium @Pulkit30