• 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 /
avatar image
0
Question by n00040418 · Apr 25, 2017 at 02:41 PM · scripting problemlevel load

Problems using OnLevelWasLoaded()

Hello, I am currently working on two aspects of my game in unity regarding the scripting for the main menu before the game is played. I am trying to make the scene change while simultaneously keeping the music going and to also have the scenes fade in and out. I am getting an error though regarding the "OnLevelWasLoaded" in both of my MusicPlayer and Fading scripts as a warning and I am getting an error for those same methods in my musicplayer script every times the scene changes. It mentions that "object reference not set to an instance of an object". I am am not sure what is causing these errors and was wondering if someone could see my error... Here is my fading script:

     using UnityEngine;
     using System.Collections;
     
     public class Fading : MonoBehaviour {
     
         public Texture2D fadeOutTexture;    // the texture that will overlay the screen. This can be a black image or a loading graphic
         public float fadeSpeed = 0.8f;        // the fading speed
     
         private int drawDepth = -1000;        // the texture's order in the draw hierarchy: a low number means it renders on top
         private float alpha = 1.0f;            // the texture's alpha value between 0 and 1
         private int fadeDir = -1;            // the direction to fade: in = -1 or out = 1
     
         void OnGUI()
         {
             // fade out/in the alpha value using a direction, a speed and Time.deltaTime to convert the operation to seconds
             alpha += fadeDir * fadeSpeed * Time.deltaTime;
             // force (clamp) the number to be between 0 and 1 because GUI.color uses Alpha values between 0 and 1
             alpha = Mathf.Clamp01(alpha);
     
             // set color of our GUI (in this case our texture). All color values remain the same & the Alpha is set to the alpha variable
             GUI.color = new Color (GUI.color.r, GUI.color.g, GUI.color.b, alpha);
             GUI.depth = drawDepth;                                                                // make the black texture render on top (drawn last)
             GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), fadeOutTexture);        // draw the texture to fit the entire screen area
         }
     
         // sets fadeDir to the direction parameter making the scene fade in if -1 and out if 1
         public float BeginFade (int direction)
         {
             fadeDir = direction;
             return (fadeSpeed);
         }
     
         // OnLevelWasLoaded is called when a level is loaded. It takes loaded level index (int) as a parameter so you can limit the fade in to certain scenes.
         void OnLevelWasLoaded()
         {
             // alpha = 1;        // use this if the alpha is not set to 1 by default
             BeginFade(-1);        // call the fade in function
         }
     }

the music player script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MusicPlayer : MonoBehaviour {
 
     private float currentMusicTime;
     private AudioSource ad;
  
     void Start()
     {
         DontDestroyOnLoad(gameObject);
         ad = GameObject.Find("Music Holder").GetComponent<AudioSource>();
     }
 
     void Update()
     {
         currentMusicTime = ad.time;
         if (currentMusicTime == 0)
         {
             ad.Play();
         }
         
     }
 
     void OnLevelWasLoaded()
     {
         ad.time = currentMusicTime;
     }
 
 
 }
 

and the script to change scenes:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 
 public class Scene : MonoBehaviour {
 
     public string ChangeToScene;
 
     public void ChangeScene(){
         float fadeTime = GameObject.Find ("Canvas").GetComponent<Fading> ().BeginFade (1);
         //yield return new WaitForSeconds (fadeTime);
         SceneManager.LoadScene (ChangeToScene);
     }
    
 }

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Puzzle level completed how to move to next level 1 Answer

Scripting Help: Timer Problem 0 Answers

A* Pathfinding on click? 0 Answers

How to attach a (that world type) canvas to a moving player? 0 Answers

How can I get the SerializedObject and FineProperty also the node tree childs of conversations in editorwindow ? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges