[英]The object of type "CanvasGroup" has been destroyed but you are still trying to access it
I was testing out some code for my game: trying to make an object already picked up to not spawn again when reloading the level.我正在为我的游戏测试一些代码:试图让一个已经被拾取的对象在重新加载关卡时不再生成。 For some reason, I get this error now and I don't seem to find a solution yet (saw many had this issues while googling but none of the fixes seemed to fit)出于某种原因,我现在收到这个错误,我似乎还没有找到解决方案(在谷歌搜索时看到很多人有这个问题,但似乎没有一个修复程序适合)
Didn't try much as it is quite personnal.没有尝试太多,因为它非常人性化。 One fix wouldn't work to me as I cannot find out where to put it exactly (due to different scripts names..)一种修复对我不起作用,因为我找不到确切的位置(由于不同的脚本名称..)
public class GameManager_GameLevel : MonoBehaviour
{
//MANAGER
private const int COIN_SCORE_AMOUNT = 5;
public static GameManager_GameLevel Instance { set; get; }
public bool isDead { set; get; }
private bool isGameStarted = false;
private PlayerMotor_GameLevel motor;
public Text coinText;
private float coinScore;
private bool gameStarted;
public GameObject player;
public GameObject cameraFollow;
private Vector3 deathPosition = new Vector3(0, 0, 30);
public Button buttonReplay;
//FADE
private CanvasGroup fadeGroup;
private float fadeInDuration = 2.0f;
//ANIMATOR
public Animator deathMenuAnim;
public Animator pauseMenuAnim;
public Animator playerAnim;
private void Awake()
{
Instance = this;
motor = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerMotor_GameLevel>();
coinText.text = coinScore.ToString("0");
}
private void Start()
{
SceneManager.LoadScene(SaveManager.Instance.currentLevel.ToString(), LoadSceneMode.Additive);
fadeGroup = FindObjectOfType<CanvasGroup>();
fadeGroup.alpha = 1;
}
public void Update()
{
if(Time.timeSinceLevelLoad <= fadeInDuration)
{
fadeGroup.alpha = 1 - (Time.timeSinceLevelLoad / fadeInDuration);
}
else if(!gameStarted)
{
fadeGroup.alpha = 0;
gameStarted = true;
}
GameStarted();
}
public void GameStarted()
{
if(MobileInput_GameLevel.Instance.Tap && !isGameStarted)
{
isGameStarted = true;
motor.StartRunning();
FindObjectOfType<DecorSpawner_GameLevel>().isScrolling = true;
FindObjectOfType<CameraMotor_GameLevel>().isMoving = true;
}
}
public void GetCoin()
{
coinScore ++;
coinText.text = coinScore.ToString("0");
}
public void OnDeath()
{
isDead = true;
FindObjectOfType<DecorSpawner_GameLevel>().isScrolling = false;
deathMenuAnim.SetTrigger("Dead");
}
public void Pause()
{
Time.timeScale = 0f;
pauseMenuAnim.SetBool("Pause", true);
}
public void ResumeGame()
{
Time.timeScale = 1f;
pauseMenuAnim.SetBool("Pause", false);
}
public void RetryButton()
{
Time.timeScale = 1f;
SceneManager.LoadScene("GameLevel");
}
public void QuitButton()
{
Time.timeScale = 1f;
SceneManager.LoadScene("MainMenu");
}
public void ReplayButton()
{
buttonReplay.gameObject.SetActive(false);
motor.StartRunning();
FindObjectOfType<DecorSpawner_GameLevel>().isScrolling = true;
FindObjectOfType<CameraMotor_GameLevel>().isMoving = true;
}
public void Respawn()
{
AdController.Instance.PlayRewardedVideo();
buttonReplay.gameObject.SetActive(true);
player.transform.position = player.transform.position - deathPosition;
cameraFollow.transform.position = cameraFollow.transform.position - deathPosition;
playerAnim.SetTrigger("Reset");
deathMenuAnim.SetTrigger("Reset");
}
}
That issue is occurring on this script, but also on others.这个问题发生在这个脚本上,但也发生在其他脚本上。 It actually occurs on any script getting called when I click a button in game, such as "restart, menu, option" after I already started playing.它实际上发生在我开始玩游戏后单击游戏中的按钮(例如“重新启动、菜单、选项”)时被调用的任何脚本上。 Everything is fine until calling a second scene actually.一切都很好,直到实际调用第二个场景。
Oh also, I don't actually destroy anything and everything was fine before.哦还有,我实际上并没有破坏任何东西,之前一切都很好。 That error started out of nowhere and I didn't even change anything on all these scripts.那个错误突然开始了,我什至没有对所有这些脚本进行任何更改。
Edit: After someone pointed out my previous title was confusing.编辑:在有人指出我以前的标题令人困惑之后。 The object type could be CanvasGroup (the first that occured) but I also saw Text, and others.对象类型可以是 CanvasGroup(出现的第一个),但我也看到了 Text 和其他类型。
Apply this to the GameObject which you want to persist between scene .将此应用到要在 Scene 之间保留的 GameObject 。
refs:参考:
var gameObject = //whatever
DontDestroyOnLoad(gameObject);
such as:如:
private void Awake()
{
DontDestroyOnLoad(fadeGroup);
Instance = this;
motor = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerMotor_GameLevel>();
coinText.text = coinScore.ToString("0");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.