I am working on a mobile game, and am experiencing a strange bug/error. When I load into the main scene , all is fine everything works and no errors. However there are two scenario's in which it breaks
- when I open the pause menu and go to the main menu. Then return to the level all is broken.
- coming directly from the main menu into the level
GameObject links in scripts are missing and some other stuff. Here are the errors and corresponding code that is used:
If some screen from the editor are required I will update the question
Code used for the pause menu:
public class Pause : MonoBehaviour {
[SerializeField]private bool _isPaused;
[SerializeField] private GameObject _attackButton;
[SerializeField] private GameObject _moveButton;
[SerializeField] private GameObject _pauseMenu;
[SerializeField] private GameObject _pauseButton;
// Use this for initialization
void Start () {
_isPaused = false;
_pauseMenu.SetActive(false);
}
// Update is called once per frame
void Update () {
}
public void OnClickPauseButton()
{
_isPaused = !_isPaused;
if(_isPaused)
{
Time.timeScale = 0;
HideButtons();
}
}
public void OnClickResumeButton()
{
_isPaused = !_isPaused;
if (!_isPaused)
{
Time.timeScale = 1;
ShowButtons();
}
}
public void OnClickMenuButton()
{
SceneManager.LoadScene("MainMenu");
}
private void HideButtons()
{
_attackButton.SetActive(false);
_moveButton.SetActive(false);
_pauseButton.SetActive(false);
_pauseMenu.SetActive(true);
}
private void ShowButtons()
{
_attackButton.SetActive(true);
_moveButton.SetActive(true);
_pauseButton.SetActive(true);
_pauseMenu.SetActive(false);
}
}
The Main menu code, that is used
public void StartGame()
{
SceneManager.LoadScene("2DScene");
}
public void ClickUnfinishedButton()
{
StartCoroutine("FadeInAndOutText");
}
IEnumerator FadeInAndOutText()
{
toBeAddedText.SetActive(true);
yield return new WaitForSeconds(2);
toBeAddedText.SetActive(false);
}
EDIT:
Scoremanager code:
public class ScoreManager : MonoBehaviour {
[SerializeField] private PlayerController _player;
[SerializeField] private GameObject text;
[SerializeField] private Slider SliderVar;
private int _currentScore, _requiredScore;
// Use this for initialization
void Start()
{
InitializeVariables();
}
private void InitializeVariables()
{
_player = _player.GetComponent<PlayerController>();
_requiredScore = 10;
}
// Update is called once per frame
void Update()
{
setScoreSlider();
text.GetComponent<Text>().text = _player._currentScore + "/" + _requiredScore;
}
private void setScoreSlider()
{
SliderVar.value = _player._currentScore / _requiredScore;
}
}
Code from inputscript:
public class InputScript : MonoBehaviour
{
public FingersJoystickScript joystickScript;
[SerializeField] private float _minimumDistanceSwipe;
[SerializeField] private float _minimumSpeedSwipe;
[SerializeField] private SwipeGestureRecognizerDirection _swipeDirection;
public GameObject player;
public bool isAttacking;
private TapGestureRecognizer _pressGestureRecognizer;
private Vector2 _smoothDirection;
private SwipeGestureRecognizer _swipeGestureRecognizer;
private LongPressGestureRecognizer _longPressGestureRecognizer;
private bool _facingRight = false;
private Animator anim;
private GestureTouch FirstTouch(ICollection<GestureTouch> touches)
{
foreach (var t in touches)
{
return t;
}
return new GestureTouch();
}
private void Awake()
{
joystickScript.JoystickExecuted = JoystickExecuted;
joystickScript.MoveJoystickToGestureStartLocation = false;
isAttacking = false;
}
// public bool MoveJoysticktoGestureStartLocation;
private void Start()
{
anim = player.GetComponent<Animator>();
CreateSwipeGesture();
_swipeGestureRecognizer.MinimumDistanceUnits = _minimumDistanceSwipe;
_swipeGestureRecognizer.MinimumSpeedUnits = _minimumSpeedSwipe;
_swipeGestureRecognizer.Direction = _swipeDirection;
}
private void Update()
{
_swipeGestureRecognizer.MinimumDistanceUnits = _minimumDistanceSwipe;
_swipeGestureRecognizer.MinimumSpeedUnits = _minimumSpeedSwipe;
_swipeGestureRecognizer.Direction = _swipeDirection;
}
private void CreateSwipeGesture()
{
_swipeGestureRecognizer = new SwipeGestureRecognizer
{
Direction = SwipeGestureRecognizerDirection.Any,
DirectionThreshold = 1.0f
};
_swipeGestureRecognizer.Updated += SwipeGestureCallback;
FingersScript.Instance.AddGesture(_swipeGestureRecognizer);
}
private void SwipeGestureCallback(GestureRecognizer gesture, ICollection<GestureTouch> touches)
{
if (gesture.State == GestureRecognizerState.Ended)
{
isAttacking = true;
}
}
private void JoystickExecuted(FingersJoystickScript script, Vector2 amount)
{
if (amount.x > 0 && !_facingRight)
{
FlipXAxis();
}
else if (amount.x < 0 && _facingRight)
{
FlipXAxis();
}
Vector3 pos = player.transform.position;
pos.x += (amount.x * 8 * Time.deltaTime);
pos.z += (amount.y * 8 * Time.deltaTime);
player.transform.position = pos;
anim.SetBool("isWalking", true);
if (amount == Vector2.zero)
{
anim.SetBool("isWalking", false);
}
}
private void FlipXAxis()
{
//oposite direction
_facingRight = !_facingRight;
//get local scale
var theScale = player.transform.localScale;
//flip on x axis
theScale.x *= -1;
//apply that to the local scale
player.transform.localScale = theScale;
}
}

InitializeVariables, you can'tGetComponentfrom a variable that is alreadyNULLand InputScript.cs shows only 8 lines of code here and your error is on line 36. \$\endgroup\$