Forum begins after the advertisement:
Search Results for 'fader'
-
Search Results
-
Hi, I have a trouble on part 6.
I just followed “Camera Bounds” & “Dynamic Camera Y Damping” and when I try to run the game, it just pauses and there are repetitive error msg on console.
NullReferenceException: Object reference not set to an instance of an object
SceneTransition.Start () (at Assets/Scripts/SceneTransition.cs:27)NullReferenceException: Object reference not set to an instance of an object
PlayerController.UpdateCameraYDampForPlayerFall () (at Assets/Scripts/PlayerController.cs:253)
PlayerController.Update () (at Assets/Scripts/PlayerController.cs:184)NullReferenceException: Object reference not set to an instance of an object
PlayerController.UpdateCameraYDampForPlayerFall () (at Assets/Scripts/PlayerController.cs:258)
PlayerController.Update () (at Assets/Scripts/PlayerController.cs:184)I don’t know how to deal with “NUllReferenceException” error.
this is my error console img below.
“Scenetransition.cs”
public class SceneTransition : MonoBehaviour { [SerializeField] private string transitionTo; //Represents the scene to transition to [SerializeField] private Transform startPoint; //Defines the player's entry point in the scene [SerializeField] private Vector2 exitDirection; //Specifies the direction for the player's exit [SerializeField] private float exitTime; //Determines the time it takes for the player to exit the scene transition // Start is called before the first frame update private void Start() { if (GameManager.Instance.transitionedFromScene == transitionTo) { PlayerController.Instance.transform.position = startPoint.position; StartCoroutine(PlayerController.Instance.WalkIntoNewScene(exitDirection, exitTime)); } StartCoroutine(UIManager.Instance.sceneFader.Fade(SceneFader.FadeDirection.Out)); } private void OnTriggerEnter2D(Collider2D _other) { if (_other.CompareTag("Player")) { GameManager.Instance.transitionedFromScene = SceneManager.GetActiveScene().name; PlayerController.Instance.pState.cutscene = true; SceneManager.LoadScene(transitionTo); StartCoroutine(UIManager.Instance.sceneFader.FadeAndLoadScene(SceneFader.FadeDirection.In, transitionTo)); } } }
“PlayerController.cs”
void Update() { if (pState.cutscene) return; GetInputs(); UpdateJumpVariables(); UpdateCameraYDampForPlayerFall(); RestoreTimeScale(); if (pState.dashing) return; Flip(); Move(); Jump(); StartDash(); Attack(); FlashWhileInvincible(); Heal(); CastSpell(); } void UpdateCameraYDampForPlayerFall() { //if falling past a certain speed threshold if(rb.velocity.y < playerFallSpeedThreshold && !CameraManager.Instance.isLerpingYDamping && !CameraManager.Instance.hasLerpedYDamping) { StartCoroutine(CameraManager.Instance.LerpYDamping(true)); } //if standing still or moving up if(rb.velocity.y >= 0 && !CameraManager.Instance.isLerpingYDamping && CameraManager.Instance.hasLerpedYDamping) { //reset camera function CameraManager.Instance.hasLerpedYDamping = false; StartCoroutine(CameraManager.Instance.LerpYDamping(false)); } }
Hi everyone,
When i try to save the “bench” coordinates and die to see if i respawn back at the correct point, it load for a second at the correct position, but then i get teleported back at starting point and an error appears
NullReferenceException: Object reference not set to an instance of an object UIManager+<DeactivateDeathScreen>d__8.MoveNext () (at Assets/Scripts/UIManager.cs:45) UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress)
Here is the code:
Bench code:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class SavePoint : MonoBehaviour { [SerializeField]public bool interacted; private void OnTriggerStay2D(Collider2D collision) { if(collision.CompareTag("Player") && Input.GetButtonDown("Interact")) { interacted = true; SaveData.Instance.stoneSceneName = SceneManager.GetActiveScene().name; SaveData.Instance.stonePos = new Vector2(gameObject.transform.position.x, gameObject.transform.position.y); SaveData.Instance.SaveStone(); } } }
Save data code:
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; using UnityEngine.SceneManagement; [System.Serializable] public struct SaveData { public static SaveData Instance; //stone data public string stoneSceneName; public Vector2 stonePos; //player data public int playerHealth; public Vector2 playerPos; public string lastScene; public void Initialize() { if(!File.Exists(Application.persistentDataPath + "/save.stone.data")) { BinaryWriter writer = new BinaryWriter(File.Create(Application.persistentDataPath + "/save.stone.data")); } } public void SaveStone() { using(BinaryWriter writer = new BinaryWriter(File.OpenWrite(Application.persistentDataPath + "/save.stone.data"))) { writer.Write(stoneSceneName); writer.Write(stonePos.x); writer.Write(stonePos.y); } } public void LoadStone() { string savePath = Application.persistentDataPath + "/save.stone.data"; if (File.Exists(savePath) && new FileInfo(savePath).Length > 0) { using(BinaryReader reader = new BinaryReader(File.OpenRead(Application.persistentDataPath + "/save.stone.data"))) { stoneSceneName = reader.ReadString(); stonePos.x = reader.ReadSingle(); stonePos.y = reader.ReadSingle(); } } } }
Game manager code:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class GameManager : MonoBehaviour { public string transitionedFromScene; public static GameManager Instance { get; private set; } public Vector2 respawnPoint; [SerializeField] SavePoint stone; private void Awake() { SaveData.Instance.Initialize(); if(Instance != null && Instance != this) { Destroy(gameObject); } else { Instance = this; } DontDestroyOnLoad(gameObject); stone = FindObjectOfType<SavePoint>(); } public void RespawnPlayer() { SaveData.Instance.LoadStone(); if(SaveData.Instance.stoneSceneName != null) { SceneManager.LoadScene(SaveData.Instance.stoneSceneName); } Debug.Log(SaveData.Instance.stonePos); if(SaveData.Instance.stonePos.x != 0 && SaveData.Instance.stonePos.y != 0) { respawnPoint.y = SaveData.Instance.stonePos.y + 1.358f; respawnPoint.x = SaveData.Instance.stonePos.x; } else { respawnPoint = new Vector2(-3f, -2.58f); } PlayerMovement.Instance.transform.position = respawnPoint; StartCoroutine(UIManager.Instance.DeactivateDeathScreen()); PlayerLife.pl.Respawn(); } }
UIManager code:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class UIManager : MonoBehaviour { [SerializeField]Slider slider; [SerializeField]PlayerLife player; public static UIManager Instance; [SerializeField] GameObject deathScreen; public GameObject inventory; public SceneFader sceneFader; private void Awake() { if(Instance != null && Instance != this) { Destroy(gameObject); } else { Instance = this; } DontDestroyOnLoad(gameObject); sceneFader = GetComponentInChildren<SceneFader>(); } public IEnumerator ActivateDeathScreen() { yield return new WaitForSeconds(0.8f); StartCoroutine(sceneFader.Fade(SceneFader.FadeDirection.In)); yield return new WaitForSeconds(0.8f); deathScreen.SetActive(true); } public IEnumerator DeactivateDeathScreen() { yield return new WaitForSeconds(0.2f); deathScreen.SetActive(false); StartCoroutine(sceneFader.Fade(SceneFader.FadeDirection.Out)); slider.value = player.maxHealth; } // Start is called before the first frame update void Start() { slider.maxValue = player.maxHealth; slider.value = player.maxHealth; } public void UpdateUIHealtbar(int currentHealth) { slider.value = currentHealth; } }
Thanks in advance to everyone
Advertisement below: