Unable to call function from another script
I have encountered a problem where my request to call a function (parses a string) from a separate script is ignored. It works fine in other scenes and from other scripts but this one just doesn't call it, no error or anything. here is my code. this is the one that calls the function: P.S Return and NextLevel are called by ui buttons and LevelComplete is called from another script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class LevelCompletion : MonoBehaviour {
Scene CurrentScene;
public string LevelSelectSceneName;
[SerializeField]
private GameObject SceneFadeManager;
public GameObject LevelCompleteUI;
public string NextLevelName;
private GameObject EndLevelTrigger;
private GameObject Player;
private void Awake()
{
EndLevelTrigger = GameObject.FindGameObjectWithTag("LevelEnd");
SceneFadeManager = GameObject.FindGameObjectWithTag("SceneFadeEngine");
}
private void Update()
{
if (EndLevelTrigger.GetComponent<EndLevelTrigger>().Istriggered == true)
{
LevelCompleted();
}
}
public void LevelCompleted ()
{
PlayerPrefs.SetInt(NextLevelName, 1); //Set the next uncompleted level to the next level
LevelCompleteUI.SetActive(true); //Enable Level Complete UI
Time.timeScale = 0f; //Freeze The game
}
public void NextLevel ()
{
//SceneManager.LoadScene(NextLevelName);
//Use Scene Fade Engine to load the next level
SceneFadeManager.GetComponent<SceneLoadFade>().LoadSceneByName(NextLevelName);
}
public void Return ()
{
//SceneManager.LoadScene(LevelSelectSceneName);
Debug.Log("Returning To "+LevelSelectSceneName);
//Use Scene Fade Engine to load the level select scene
SceneFadeManager.GetComponent<SceneLoadFade>().LoadSceneByName("Pack1");
}
}
The Debug.Log part works fine but the function call does nothing. I have confirmed that it is not a reference issue. Here is the code that contains the function: P.S OnFadeComplete is called by the Animation/Animator when the animation is finished
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneLoadFade : MonoBehaviour {
//Reference to animator
private Animator FadeAnim;
//Enum to store whether we are loading the scene by its build index (int) or it's name (string)
public enum SceneLoadMode {Name, Index}
//String name of scene to load
private string SceneName;
//Interger index of scene to load
private int SceneIndex;
//Instantiating the SceneLoadMode enum
private SceneLoadMode sceneLoadMode;
private void Awake()
{
FadeAnim = GetComponent<Animator>();
}
//Called when scene is being loaded by name
public void LoadSceneByName (string _sceneName)
{
FadeAnim.SetTrigger("FadeOut"); //Trigger fade out animation
SceneName = _sceneName; //Store name of scene for later use
sceneLoadMode = SceneLoadMode.Name;
}
//Called when scene is being loaded by its build index
public void LoadSceneByIndex (int index)
{
FadeAnim.SetTrigger("FadeOut"); //Trigger fade out animation
SceneIndex = index; //Store index of scene for later use
sceneLoadMode = SceneLoadMode.Index;
}
//Called when fade out animation finishes
public void OnFadeComplete()
{
//Load scene
if (sceneLoadMode == SceneLoadMode.Name)
{
Debug.Log("Loading Scene By Name: " + SceneName);
SceneManager.LoadScene(SceneName);
}
else if (sceneLoadMode == SceneLoadMode.Index)
{
Debug.Log("Loading Scene By Index: " + SceneIndex);
SceneManager.LoadScene(SceneIndex);
}
}
}
Does anybody know what is wrong?
Your answer
Follow this Question
Related Questions
My Turrent Trigger does not work 1 Answer
Damage Text pop-up when enemy is hit 1 Answer
IENumerator does not work 0 Answers
Mathf.PerlinNoise does not work 0 Answers