- Home /
The variable has not been assigned (but it has)
I've started working on a new horror game idea, and with that idea i need doors. I coded the UI and functionality for the door in this script:
public class DoorCellOpen : MonoBehaviour {
public float TheDistance;
public GameObject ActionDispaly;
public GameObject ActionText;
public GameObject TheDoor;
public AudioSource CreakSound;
public GameObject ExtraCross;
void Update () {
TheDistance = PlayerCasting.DistanceFromTarget;
}
void OnMouseOver () {
if (TheDistance <= 2.5) {
ExtraCross.SetActive (true);
ActionDispaly.SetActive (true);
ActionText.SetActive (true);
ActionText.GetComponent<Text> ().text = "Open Door";
}
if (Input.GetButtonDown("Action")) {
if (TheDistance <= 2.5) {
this.GetComponent<BoxCollider>().enabled = false;
ActionDispaly.SetActive (false);
ActionText.GetComponent<Text> ().text = "Open Door";
TheDoor.GetComponent<Animation> ().Play ("firstdooropenanim");
CreakSound.Play ();
}
}
}
void OnMouseExit() {
ExtraCross.SetActive (false);
ActionDispaly.SetActive (false);
ActionText.SetActive (false);
}
}
the program worked like a charm. But, i also needed a door to change from level 1 to level 2 with nearly the same function. I changed the display text, took out the animation component and made some basic changes, as well as removed the previous script from my object and replaced it with the new one. on playtesting, the item still ran through its old animation and displayed "Open Door" on screen. The scene didnt change and the only compiler error was:
UnassignedReferenceException: The variable ExtraCross of DoorCellOpen has not been assigned. You probably need to assign the ExtraCross variable of the DoorCellOpen script in the inspector. DoorCellOpen.OnMouseExit () (at Assets/scripts/DoorCellOpen.cs:38) UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)
Even though the object doesn't even have DoorCellOpen (the first script) attached! Here's the script:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Scene002Door : MonoBehaviour { public float TheDistance; public GameObject ActionDispaly; public GameObject ActionText; public GameObject TheDoor; public AudioSource CreakSound; public GameObject ExtraCross;
void Update () {
TheDistance = PlayerCasting.DistanceFromTarget;
}
void OnMouseOver () {
if (TheDistance <= 2.5) {
ExtraCross.SetActive (true);
ActionDispaly.SetActive (true);
ActionText.SetActive (true);
ActionText.GetComponent<Text> ().text = "Enter Todeshalle";
}
if (Input.GetButtonDown("Action")) {
if (TheDistance <= 2.5) {
ActionDispaly.SetActive (false);
CreakSound.Play ();
SceneManager.LoadScene (5);
}
}
}
void OnMouseExit() {
ExtraCross.SetActive (false);
ActionDispaly.SetActive (false);
ActionText.SetActive (false);
}
}
If you need any further info just ask. Thank you.
Uhh, i see ExtraCross in your second script as well...
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Controlling Players Mouse 2 Answers
Renderer on object disabled after level reload 1 Answer
Door is glitching open and close 1 Answer