Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Jul 06, 2016 at 07:29 AM by El-Deiablo for the following reason:

Other

avatar image
0
Question by El-Deiablo · Jul 01, 2016 at 12:18 PM · coroutinenullreferenceexceptionreferencenull reference exceptioncoroutine errors

Coroutine Issue

I have been trying to debug and cannot figure out this issue:

NullReferenceException: Object reference not set to an instance of an object PausePanel+c__Iterator1.MoveNext () (at Assets/Scripts/Background Scripts/PausePanel.cs:261) UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator) PausePanel:YESQUIT() (at Assets/Scripts/Background Scripts/PausePanel.cs:151) UnityEngine.EventSystems.EventSystem:Update()

This is the line of code with the issue: float fadeTime = GameObject.Find ("Scene Fader").GetComponent ().BeginFade (1);

I have checked the spelling of my gameObject and it is correct and the the Scene Fader gameobject has the SceneFader script attached to it.

I have buttons that transition between levels - Alot of the tutorials assume you have your level system set up progressively so I'm having trouble adjusting to my setup.

Here's my code:

SceneFader:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.SceneManagement;
 
 public class SceneFader : MonoBehaviour {
 
     public Texture2D fadeOutTexture;
     public float fadeSpeed = 0.8f;
 
     private int drawDepth = -1000;
     private float alpha = 1.0f;
     private int fadeDir = -1;
 
 
     void OnGUI(){
 
         alpha += fadeDir * fadeSpeed * Time.deltaTime;
         alpha = Mathf.Clamp01 (alpha);
         GUI.color = new Color (GUI.color.r, GUI.color.g, GUI.color.b, alpha);
         GUI.depth = drawDepth;
         GUI.DrawTexture(new Rect (0,0,Screen.width ,Screen.height),fadeOutTexture);
 
     }
 
     public float BeginFade(int direction){
 
         fadeDir = direction;
         return (fadeSpeed);
 
     }
 
     void OnLevelWasLoaded(){
 
         BeginFade (-1);
 
     }
 }

PausePanel:

using System.Collections; using UnityEngine.SceneManagement; using UnityEngine.UI;

public class PausePanel : MonoBehaviour {

 public static bool isPaused=false;

 public GameObject pauseMenuCanvas;

 void Start(){

 }
 
 // Update is called once per frame
 void Update () {
     
     if (isPaused == true) {

         pauseMenuCanvas.SetActive (true);
         Time.timeScale = 0f;

     } else if (isPaused==false && TimeChangingPowerUp.Active == true) {

         pauseMenuCanvas.SetActive(false);
         Time.timeScale = 0.5f;

     }

     else {

         pauseMenuCanvas.SetActive(false);
         Time.timeScale = 1f;

     }
         
 }

 public void Restart(){

     AreYouSureYouWantToRestart.SetActive (true);

 }

 public void YESRESTART(){

     //SceneManager.LoadScene ("Gameplay");
     //SceneFader.instance.LoadLevel("Gameplay");
     StartCoroutine (SceneFaderGamePlay());

 }

 public void YESQUIT(){

     //SceneFader.instance.LoadLevel("Title Menu");
     //SceneManager.LoadScene ("Title Menu");
     StartCoroutine (SceneFaderHome());

 }

 public void Pause(){

     isPaused = true;
     pauseMenuCanvas.SetActive(true);
     Time.timeScale = 0f;

 }

 IEnumerator SceneFaderGamePlay(){

     float fadeTime = GameObject.Find ("Scene Fader").GetComponent<SceneFader> ().BeginFade (1);
     yield return new WaitForSeconds (fadeTime);
     SceneManager.LoadScene ("Gameplay");
     isPaused = false;

 }

 IEnumerator SceneFaderHome(){

     float fadeTime = GameObject.Find ("Scene Fader").GetComponent<SceneFader> ().BeginFade (1);
     yield return new WaitForSeconds (fadeTime);
     SceneManager.LoadScene ("Title Menu");
     isPaused = false;


 }


}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tanoshimi · Jul 01, 2016 at 12:31 PM 0
Share

The error message is reporting a problem at line 261 of PausePanel.cs. You've only posted 80 lines....

1 Reply

  • Sort: 
avatar image
0

Answer by Bunny83 · Jul 01, 2016 at 12:38 PM

Inside your coroutine you only have one line that actually involves references which could be null and that is:

 float fadeTime = GameObject.Find ("Scene Fader").GetComponent<SceneFader> ().BeginFade (1);

The possible reasons are:

  • There is no gameobject called "Scene Fader" and thus GameObject.Find returns "null".

  • If there is a gameobject with that name the next thing is that it doesn't have a SceneFader script attached and GetComponent returns "null".

If a reference is "null" you can't use it for anything. So in the first case when there is no gameobject the GetComponent call will cause a Null-Ref-Exception. In the second case, when there is a gameobject with that name but GetComponent returns null the call to BeginFade will cause a Null-Ref-Exception.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

46 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why isn't my coroutine working when I call it from another script. 0 Answers

Null Reference 1 Answer

Coroutine working in Play Mode, but not on build 0 Answers

FindObjectsOfType giving out null 1 Answer

Need some help with networking, Object reference not set to an instance of an object. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges