UnassignedReferenceException (Didn't find a solution)
I'm a beginner on unity and I have this problem: UnassignedReferenceException: The variable TEXT1 of Player has not been assigned
And this is my main script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Player : MonoBehaviour {
public GameObject camcam;
public GameObject score;
public GameObject Button;
public GameObject PANEL1;
public GameObject TEXT1;
public GameObject TEXT2;
public int scoremac;
public double Ytodie;
public string NextLVL;
public string ThisLVL;
public static int pointage = 0;
void Start() {
PANEL1 = GameObject.FindWithTag("Panel");
TEXT1 = GameObject.FindWithTag("txt1");
TEXT2 = GameObject.FindWithTag("txt2");
pointage = 0;
PANEL1.SetActive(false);
TEXT1.SetActive(false);
TEXT2.SetActive(false);
score.GetComponent<Text>().text = "Score: " + pointage;
Button.SetActive(false);
}
// Update is called once per frame
void Update()
{
AdjustCam();
CheckKeys();
CheckScore();
CheckifFall();
}
bool boff = false;
void CheckifFall()
{
if(boff == false)
{
if (gameObject.transform.position.y < Ytodie)
{
StartCoroutine(Timer2());
}
}
}
public void OnClick()
{
TEXT1.SetActive(true);
TEXT2.SetActive(true);
PANEL1.SetActive(true);
StartCoroutine(LoadLevelsPro());
}
IEnumerator Timer2()
{
yield return new WaitForSeconds(1);
pointage = 0;
SceneManager.LoadScene(ThisLVL);
}
IEnumerator LoadLevelsPro()
{
Button.SetActive(false);
yield return new WaitForSeconds(1);
AsyncOperation loading = SceneManager.LoadSceneAsync(NextLVL);
while (!loading.isDone)
{
yield return null;
}
}
void CheckScore()
{
if (pointage <= scoremac)
{
score.GetComponent<Text>().text = "Score: " + pointage;
}
else
{
score.GetComponent<Text>().color = Color.green;
score.GetComponent<Text>().text = "Victoire!";
boff = true;
Button.SetActive(true);
}
}
void CheckKeys()
{
float mouvHorizontal = Input.GetAxis("Horizontal");
float mouvVertical = Input.GetAxis("Vertical");
Vector3 mouvement = new Vector3(mouvHorizontal, 0, mouvVertical);
gameObject.GetComponent<Rigidbody>().AddForce(mouvement * 120 * Time.deltaTime);
}
void AdjustCam()
{
camcam.transform.position = new Vector3(gameObject.transform.position.x,
gameObject.transform.position.y + 5,
gameObject.transform.position.z - 10);
}
}
This is an example:
Answer by Backside2357 · Jan 20, 2018 at 08:57 PM
This error means that your TEXT1 variable is null. This means it doesn't point to a valid object, which means in your case, that the TEXT1 = GameObject.FindWithTag("txt1");
call failed or wasn't even called in the first place. When you do stuff like that you should always check for null
before you use the object:
if(TEXT1 == null)
{
Debug.LogError("TEXT1 is null");
return;
}
Your answer
Follow this Question
Related Questions
changing a public variable of a prefab with a script. 0 Answers
My GetComponent is not working. 1 Answer
UnassignedReferenceException: The variable has not been assigned, even though it has. - c# 3 Answers
I got three CS 1061 Errors while learning Unity as an absolute beginner. Please help me! 1 Answer
Camera Toggling Error 1 Answer