- Home /
Can someone explain this error?
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 think u have not given the reference to the game object . This kind of error usually occurs when u have initialized a variable ,used it but didn't give reference to the variable. Double click on the error it will show u the variable whose reference is missing.
Can you copy-paste the full error message?
I don't believe the problem come from the given code.
Do you instantiate something somewhere? The error indicates a problem when calling InstantiateSingleWithParent
. Are you sure you give a non-null`parent` when calling Instantiate
?
Your question is too lack of information, please provide us more info so that we can help you easier
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.
Your question needs more information.
A guess would be that line 16:
fScreen = GameObject.Find ("EndCardScreen") as GameObject;
fails because it can't find a GameObject called "EndCardScreen". $$anonymous$$ake sure there actually is a GameObject with that name in the scene. Also, why are you casting it to a GameObject? GameObject.Find already returns a GameObject type, you don't need to cast it.
i know i don't need to cast it as a gameobject but its been giving me a lot of problems so i was trying things to verify for a fact that it was beco$$anonymous$$g a gameobject.
Answer by MajorSmurf · Aug 12, 2018 at 02:33 PM
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LevelGenerator : $$anonymous$$onoBehaviour {
public Texture2D map;
public ColorToPrefab[] colour$$anonymous$$appings;
public Transform mapHolder;
public GameObject Back;
// Use this for initialization
void Start ()
{
Generate$$anonymous$$ap ();
}
void Generate$$anonymous$$ap()
{
for (int x = 0; x < map.width; x++)
{
for (int y = 0; y < map.height; y++)
{
GenerateTile (x, y);
}
}
}
void GenerateTile(int x, int y)
{
Color pixelColour = map.GetPixel (x, y);
if (pixelColour.a == 0)
{
return;
}
foreach (ColorToPrefab colour$$anonymous$$apping in colour$$anonymous$$appings)
{
Vector3 backPosition = new Vector3 (x, y, 1.1f);
Instantiate (Back, backPosition, Quaternion.identity, mapHolder);
if (colour$$anonymous$$apping.colour.Equals (pixelColour))
{
Vector3 position = new Vector3 (x, y, 0);
Instantiate (colour$$anonymous$$apping.prefab, position, Quaternion.identity, mapHolder);
}
}
Debug.Log (pixelColour);
}
}
This is the code used to generate the level.
Don't use GetComponent<Text>();
,as there are multiple text objects you are getting in this way. Assign them in the inspector, as how will unity know which Text object you are referring to when you are setting so many variables using GetComponent<Text>();
!!
Ok I'm going to be as clear as possible.... this code 100% works as long as the game object endblock isn't a prefab which is a problem as i want the level to be generated from a png image to make levels easy and quick to design. Don't believe me look at this works 100% which is why I'm confused and need some guidance.
any solution? i was thinking maybe i need to put the scripts in a certain execute order? so the block prefab 100% spawns before the ui script
Your answer
![](https://koobas.hobune.stream/wayback/20220612172637im_/https://answers.unity.com/themes/thub/images/avi.jpg)