Hey there. I bumped into a problem with my codes. Tried searching for an answer, but to no avail, since I've tried many solutions but failed. Here's my code. I'm trying to get the value of the godMode boolean, to be used in the birdFly script. But I keep getting the NullReferenceException error. Code (CSharp): using UnityEngine; using System.Collections; public class MenuText : MonoBehaviour { public bool quit = false; public bool godMode = false; void OnMouseEnter(){ renderer.material.color = Color.red; } void OnMouseExit(){ renderer.material.color = Color.white; } void OnMouseDown(){ if (quit) Application.Quit (); else AutoFade.LoadLevel(1 ,1,1,Color.black); } } Code (CSharp): using UnityEngine; using System.Collections; public class BirdFly : MonoBehaviour { //public bool cheatMode = false; Animator animator; // Use this for initialization void Start () { animator = transform.GetComponentInChildren<Animator> (); if (animator == null) { Debug.LogError("No animator found!"); } } void OnCollisionEnter2D(Collision2D collision) { if ((GameObject.FindGameObjectWithTag("GodModeButton").GetComponent<MenuText>().godMode == true)) return; animator.SetTrigger ("dead"); dead = true; } } Hopefully I can get some insights to solving this problem. Thanks in advance!
There are two reasons why you could get a null reference exception. There is no gameobject with the GodModeButton tag in the scene or if it exists it doesn't have a MenuText component.
There are both present. Both the GodModeButton object and also the Menu Text component. The BirdFly script is used by the player in another scene, but I don't think different scenes could cause this error. Or am I wrong?
Both the BirdFly script and the MenuText script need to be in the same scene. When you try to get the GodModeButton component it returns null because it doesn't exist, it's in a different scene which is not loaded.