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