So I need a quick way to just reload the current scene with
void onReloadScene(InputAction.CallbackContext context)
{
Scene scene = SceneManager.GetActiveScene();
SceneManager.LoadScene(scene.name);
}
I get this error message
MissingReferenceException: The object of type 'CanvasGroup' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
I’m super new to programming but I’m assuming DOTween tries to animate the fading even tho all element havent been loaded yet? Is there a quick fix? I tried using the singlton pattern ‘DontDestroy’ on the UI gameobject (that has the canvas group) but it didn’t work. Could I potentially trigger DOTween slightly later?
Thanks for the help in advance!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using TMPro;
using UnityEngine.UI;
public class DistanceCalculation : MonoBehaviour
{
[SerializeField] GameObject closestPainting;
[SerializeField] float distanceToPainting;
[SerializeField] TextMeshProUGUI titleText;
[SerializeField] TextMeshProUGUI descriptionText;
[SerializeField] CanvasGroup fadingDescriptionText;
[SerializeField] CanvasGroup fadingTitleText;
[SerializeField] CanvasGroup fadingScrim;
[SerializeField] CanvasGroup fadingRawImageForQRCode;
[SerializeField] RawImage RawImageReferenceForQRCode;
[SerializeField] string[] paintingTitle =
{
"The Family",
"Nu au Voile",
"The Dance Class"
};
[SerializeField] string[] paintingDescription =
{
"The Kiss ...",
"Albert Aublet was a pupil ...",
"This work and its ..."
};
[SerializeField] Texture[] paintingQRCodes;
// Update is called once per frame
void Update()
{
FindClosestPainting();
if (distanceToPainting < 20f)
{
switch (closestPainting.name)
{
case "Klimt":
titleText.SetText(paintingTitle[0]);
descriptionText.SetText(paintingDescription[0]);
RawImageReferenceForQRCode.texture = paintingQRCodes[0];
FadeIn();
// Debug.Log("The Familiy");
break;
case "Lady":
titleText.SetText(paintingTitle[1]);
descriptionText.SetText(paintingDescription[1]);
RawImageReferenceForQRCode.texture = paintingQRCodes[1];
FadeIn();
// Debug.Log("Lady");
break;
case "Dance Class":
titleText.SetText(paintingTitle[2]);
descriptionText.SetText(paintingDescription[2]);
RawImageReferenceForQRCode.texture = paintingQRCodes[2];
FadeIn();
// Debug.Log("Dance Class");
break;
case null:
Debug.Log("Something went wrong");
break;
}
}
else
{
FadeOut();
}
}
public GameObject FindClosestPainting()
{
GameObject[] paintings;
paintings = GameObject.FindGameObjectsWithTag("Painting");
GameObject closest = null;
float distance = Mathf.Infinity;
Vector3 position = transform.position;
foreach (GameObject painting in paintings)
{
Vector3 diff = painting.transform.position - position;
float curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
closest = painting;
distance = curDistance;
}
}
closestPainting = closest;
distanceToPainting = distance;
return closest;
}
void FadeIn()
{
fadingScrim.DOFade(1,1);
fadingTitleText.DOFade(1,2);
fadingDescriptionText.DOFade(1,2);
fadingRawImageForQRCode.DOFade(1,2);
}
void FadeOut()
{
fadingScrim.DOFade(0,1);
fadingDescriptionText.DOFade(0,1);
fadingTitleText.DOFade(0,1);
fadingRawImageForQRCode.DOFade(0,1);
}
}