Hello, unfortunately I now have a new problem but I don’t know why.
It worked a few days ago, but unfortunately I overwrote something in the portal script, now the game always breaks off when entering the portal. It’s annoying that everything was working fine. Could someone give me some advice on this? I thank you in advance.
NullReferenceException: Object reference not set to an instance of an object
RPG.SceneManagement.Portal+d__8.MoveNext () (at Assets/Script/SceneManagment/Portal.cs:40)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <4746c126b0b54f3b834845974d1a9190>:0)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
RPG.SceneManagement.Portal:OnTriggerEnter(Collider) (at Assets/Script/SceneManagment/Portal.cs:25)
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using RPG.SceneManagement;
namespace RPG.SceneManagement
{
public class Portal : MonoBehaviour
{
enum DestinationIdentifier
{
A, B, C, D, E
}
[SerializeField] int sceneToLoad = -1;
[SerializeField] Transform spawnPoint;
[SerializeField] DestinationIdentifier destination;
[SerializeField] float fadeOutTime = 1f;
[SerializeField] float fadeInTime = 2f;
[SerializeField] float fadeWaitTime = 0.5f;
private void OnTriggerEnter(Collider other) {
if (other.tag == "Player")
{
StartCoroutine(Transition());
}
}
private IEnumerator Transition()
{
if (sceneToLoad < 0)
{
Debug.LogError("Scene to load not set.");
yield break;
}
DontDestroyOnLoad(gameObject);
Fader fader = FindObjectOfType<Fader>();
yield return fader.FadeOut(fadeOutTime);
yield return SceneManager.LoadSceneAsync(sceneToLoad);
Portal otherPortal = GetOtherPortal();
UpdatePlayer(otherPortal);
yield return new WaitForSeconds(fadeWaitTime);
yield return fader.FadeIn(fadeInTime);
Destroy(gameObject);
}
private void UpdatePlayer(Portal otherPortal)
{
GameObject player = GameObject.FindWithTag("Player");
player.transform.position = otherPortal.spawnPoint.position;
player.transform.rotation = otherPortal.spawnPoint.rotation;
}
private Portal GetOtherPortal()
{
foreach (Portal portal in FindObjectsOfType<Portal>())
{
if (portal == this) continue;
if (portal.destination != destination) continue;
return portal;
}
return null;
}
}
}
using System.Collections;
using UnityEngine;
using RPG.SceneManagement;
namespace RPG.SceneManagement{
public class Fader : MonoBehaviour
{
CanvasGroup canvasGroup;
private void Start() {
canvasGroup = GetComponent<CanvasGroup>();
StartCoroutine(FadeOutIn());
}
IEnumerator FadeOutIn()
{
yield return FadeOut(2f);
print("Faded out");
yield return FadeIn(1f);
print("Faded in");
}
public IEnumerator FadeOut(float time)
{
if(canvasGroup == null) {
yield break;
}
while (canvasGroup.alpha < 1)
{
canvasGroup.alpha += Time.deltaTime / time;
yield return null;
}
}
public IEnumerator FadeIn(float time)
{
if(canvasGroup == null) {
yield break;
}
while (canvasGroup.alpha > 0)
{
canvasGroup.alpha -= Time.deltaTime / time;
yield return null;
}
}
}
}
I also added moving grass when colliding the player, maybe there is a problem here.
No error is displayed there.
Thank you i make a video for this.