Randomly choosing scene from an array breaks the fade out
Hello! My working level change script currently fades into the next level:
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
public class LevelChange : MonoBehaviour {
public string loadLevel;
public Color loadToColor = Color.white;
void OnTriggerEnter(Collider other)
{
if (other.CompareTag ("Player"))
{
Initiate.Fade(loadLevel, loadToColor, 0.5f);
}
}
}
However, I would like to randomize the level selection between two levels, so I used an array:
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
public class RandomizedLevelChange : MonoBehaviour {
public string[] loadLevel = new string[2] {"believer", "nonbeliever"};
public Color loadToColor = Color.white;
void OnTriggerEnter(Collider other)
{
if (other.CompareTag ("Player"))
{
Initiate.Fade(loadLevel[Random.Range(0, 2)], loadToColor, 0.5f);
}
}
}
Now, the randomized level selection works, but this somehow breaks the fade script. Any ideas? I'm new to coding so I'm sure I'm missing something straightforward.
Answer by gosubis22 · Dec 25, 2017 at 12:38 AM
Can you post the fade script?
using UnityEngine;
using System.Collections;
using UnityEngine.Scene$$anonymous$$anagement;
public class Fader : $$anonymous$$onoBehaviour {
[HideInInspector]
public bool start = false;
[HideInInspector]
public float fadeDamp = 0.0f;
[HideInInspector]
public string fadeScene;
[HideInInspector]
public float alpha = 0.0f;
[HideInInspector]
public Color fadeColor;
[HideInInspector]
public bool isFadeIn = false;
//Set callback
void OnEnable()
{
Scene$$anonymous$$anager.sceneLoaded += OnLevelFinishedLoading;
}
//Remove callback
void OnDisable()
{
Scene$$anonymous$$anager.sceneLoaded -= OnLevelFinishedLoading;
}
//Create a texture , Color it, Paint It , then Fade Away
void OnGUI () {
//Fallback check
if (!start)
return;
//Assign the color with variable alpha
GUI.color = new Color (GUI.color.r, GUI.color.g, GUI.color.b, alpha);
//Temp Texture
Texture2D myTex;
myTex = new Texture2D (1, 1);
myTex.SetPixel (0, 0, fadeColor);
myTex.Apply ();
//Print Texture
GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), myTex);
//Fade in and out control
if (isFadeIn)
alpha = $$anonymous$$athf.Lerp (alpha, -0.1f, fadeDamp * Time.deltaTime);
else
alpha = $$anonymous$$athf.Lerp (alpha, 1.1f, fadeDamp * Time.deltaTime);
//Load scene
if (alpha >= 1 && !isFadeIn) {
Scene$$anonymous$$anager.LoadScene(fadeScene);
DontDestroyOnLoad(gameObject);
} else
if (alpha <= 0 && isFadeIn) {
Destroy(gameObject);
}
}
void OnLevelFinishedLoading(Scene scene, LoadScene$$anonymous$$ode mode)
{
//We can now fade in
isFadeIn = true;
}
}
and the initiate script:
using UnityEngine;
using System.Collections;
public static class Initiate {
//Create Fader object and assing the fade scripts and assign all the variables
public static void Fade (string scene,Color col,float damp){
GameObject init = new GameObject ();
init.name = "Fader";
init.AddComponent<Fader> ();
Fader scr = init.GetComponent<Fader> ();
scr.fadeDamp = damp;
scr.fadeScene = scene;
scr.fadeColor = col;
scr.start = true;
}
}
Your answer
Follow this Question
Related Questions
How to get images from an array visible 0 Answers
Assign role randomly from array for my online game 2 Answers
Creating an Array that can be accessed by a Random.Range c# 1 Answer
How can I assign swipe positions to the array as parameters? 0 Answers
Random.Range throws StackOverFlow exception in recursion 1 Answer