Hello,
I have tried to solve this and researched similar issues for 2 days now, and haven’t been able to figure it out.
I have project with a Game scene, which additively loads Navigation and Home asynchronously. Up until this point things have been going smoothly, but I’m now trying to trigger some animations on UGUI elements in Navigation based on Home’s OnEnable/OnDisable (Home gets changed out with different scenes through the navigation). It’s at this point I discovered reference errors in my build, using a nifty in-build console plug-in.
I have been using code like this:
navigationAnimationManager = GameObject.Find("NavigationManager").GetComponent<NavigationAnimationManager>();
which has worked nicely in play mode, but when I run the build I get a null reference exception. The weird part is that is that I’m getting the NRE even though my debug log says that it found the component, i.e. not null or blank.
I’ve tested this a number of ways, and they all fail the exact same way:
- I’ve tried manually setting the reference through a
public variable in the editor, on the
same object, just to see if it would
work. - I’ve tried using a getcomponent on the same object.
- I’ve tried setting a tag and loading the object through the tag.
- I’ve tried using the same code in a new script, in a new scene, on a different object (still referencing NavigationAnimationManager)
- I’ve tried using a singleton defined in Game
- I’ve tried random things like changing OnEnable to Start, loading the scenes synchronously, or waiting for the scenes to finish loading with a coroutine.
None of these have worked, when I’m pretty sure a lot of these are valid ways to do things. I get the Null Reference Exception every time. Anyone have an idea??? I’m running out.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class HomeManager : MonoBehaviour
{
private NavigationAnimationManager navigationAnimationManager;
[SerializeField] private ScrollRect scrollContentScrollRect;
private void OnEnable()
{
navigationAnimationManager = GameObject.Find("NavigationManager").GetComponent<NavigationAnimationManager>();
if (GameManager.homeController.firstTimeOnHome)
{
scrollContentScrollRect.verticalNormalizedPosition = 30.0f;
}
else
{
navigationAnimationManager.TriggerSettingsButtonOn();
scrollContentScrollRect.verticalNormalizedPosition = GameManager.homeController.scrollPosition;
}
}
private void OnDisable()
{
navigationAnimationManager.TriggerSettingsButtonOff();
//Store scroll position and set flag to trigger some animations
GameManager.homeController.scrollPosition = scrollContentScrollRect.verticalNormalizedPosition;
GameManager.homeController.firstTimeOnHome = false;
}
}