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 /
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.

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

2D game kit Door isn't opening 3 Answers

Ways to make OnMouseDown (or a single script) differentiate between different colliders/sprites 1 Answer

How can I make that the waiting time in StartCoroutine will take effect when changing the value in inspector when the game is running ? 1 Answer

variable not being changed outside of a method. [C#] 3 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