Question by
starrtennis · Jan 13, 2019 at 10:43 PM ·
2dsceneslevelsintro
Cannot access LevelChanger to change level from the pan/delay coroutine in CameraPanForIntro
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraPanForIntro : MonoBehaviour {
public Transform cameraPos;
public float panSpeed;
LevelChanger lc;
// Use this for initialization
void Start () {
lc = GetComponent<LevelChanger>();
StartCoroutine (waitForPan (lc));
}
// Update is called once per frame
void Update () {
cameraPos.position = new Vector3 (cameraPos.position.x, cameraPos.position.y - panSpeed*Time.fixedDeltaTime, cameraPos.position.z);
}
IEnumerator waitForPan(LevelChanger lc){
yield return new WaitForSeconds (10);
Debug.Log ("Done waiting");
//LevelChanger.fadeToLevel (3); //does not work; object reference is required to access non-static members levelToLoad and animator
lc.fadeToLevel(3);
}
}
using UnityEngine.SceneManagement;
using UnityEngine;
public class LevelChanger : MonoBehaviour {
public Animator animator;
private int levelToLoad;
public void fadeToLevel (int levelIndex){
levelToLoad = levelIndex;
animator.SetTrigger("FadeOut");
}
// Update is called once per frame
void Update () {
/*if (Input.GetMouseButtonDown (0)) {
FadeToLevel (3);
}*/
}
public void onFadeComplete(){
SceneManager.LoadScene (levelToLoad);
}
}
I am trying, with the two classes above, to pan a camera for ten seconds on an image in my game, then change the scene (level) with a fade. The fade and levelchange methods work (I tested them with a mouseclick trigger), but I can't seem to access them correctly from the class which is in charge of the camera pan and delay during the camera pan.
NullReferenceException: Object reference not set to an instance of an object
CameraPanForIntro+<waitForPan>c__Iterator0.MoveNext () (at Assets/Scripts/CameraPanForIntro.cs:27)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
Thanks.
UPDATE:
I solved the problem by moving the delay coroutine to the LevelChanger itself.
using UnityEngine.SceneManagement;
using UnityEngine;
using System.Collections;
public class LevelChanger : MonoBehaviour {
public Animator animator;
private int levelToLoad;
public void Start(){
StartCoroutine(waitForPan ());
}
public void fadeToLevel (int levelIndex){
levelToLoad = levelIndex;
animator.SetTrigger("FadeOut");
}
// Update is called once per frame
void Update () {
/*if (Input.GetMouseButtonDown (0)) {
FadeToLevel (3);
}*/
}
public void onFadeComplete(){
SceneManager.LoadScene (levelToLoad);
}
private IEnumerator waitForPan(){
yield return new WaitForSeconds (10);
Debug.Log ("Done waiting in LevelChanger.cs");
//LevelChanger.fadeToLevel (3); //does not work; object reference is required to access non-static members levelToLoad and animator
fadeToLevel(4);
}
}
levelchanger.jpg
(508.4 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612185713im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to Manage Objects Common for Multiple Levels? 2 Answers
How to passing levels automatically in games? 0 Answers
How to have a Next Level and Game Over pop up in scene please help! 1 Answer
Problems with my playerprefs. and Level Lock System 0 Answers
Communicate between scenes with prefabs? 3 Answers