- Home /
Update() method is not called
I am trying to implement a pause screen in my game; one of the things that it includes is a title screen button. I have the following script attached to the button sprite:
using UnityEngine;
using System.Collections;
public class TitleScreenButton : MonoBehaviour {
public Player character;
private bool touched;
void Start () {
touched = true;
}
// Update is called once per frame
void Update () {
Debug.Log("Script is running");
if (Input.touchCount == 1 && touched == false) // If the title screen button is pressed
{
Debug.Log("Touch detected");
touched = true;
Vector3 wp = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
Vector2 touchPos = new Vector2(wp.x, wp.y);
if (collider2D == Physics2D.OverlapPoint(touchPos)) // Go to title screen
{
Debug.Log("Title screen button pressed");
Application.LoadLevel("StartScreen");
}
}
if(Input.touchCount == 0)
touched = false;
}
}
The Player type refers to the script attached to the player character. (Thanks to another Unity Answers thread for the structure of the Update() method!)
I use the following if-else loop for detecting when the game is paused. Since only the else-if branch is important to this question and the if branch is quite long, I have omitted the code of the if and else branches:
if(endingState == EndState.Falling && paused == false) // Move only if the game is in motion
{
...
}
else if(paused == true) // Don't move the character, but show the objects that should be shown upon a paused game
{
endPoint.Set(startPoint.x, startPoint.y, startPoint.z); // For scoring
cameraEndPoint.Set(cameraStartPoint.x, cameraStartPoint.y, cameraStartPoint.z);
newPauseButtonPosition.Set(pauseButtonPosition.x, pauseButtonPosition.y, pauseButtonPosition.z); // Because the pause button is a sprite, it has to move with the camera
titleScreenButtonPosition = new Vector3(transform.position.x,(transform.position.y - 3));
pauseButton.gameObject.SetActive(false);
unpauseButton.gameObject.SetActive(true);
pausedTitle.gameObject.SetActive(true);
pausedTitle.transform.position = new Vector3(transform.position.x,(transform.position.y + 2));
titleScreenButton.gameObject.SetActive(true);
titleScreenButton.transform.position = titleScreenButtonPosition;
}
else // Unpaused and not falling
{
...
}
The last two lines of the else-if branch display the title screen button, which works as intended. However, the title screen button's Update() method never executes. I put the first Debug.Log() call in to test for exactly that, and the words "Script is running" did not show in the console.
What makes this even more weird is that there is another time when the title screen button is shown: when the game is over. The same sprite is used both for when the game is paused and when the game is over, but when the game is over, the button's Update() method actually works. For the sake of completeness, here is the code for displaying the button upon a game over:
if(resetButtonEnabled == true) // Does the reset or continue button need to be shown? If so, show it and the title screen button
{
resetButtonPosition = new Vector3(transform.position.x,(transform.position.y - 2));
titleScreenButtonPosition = new Vector3(resetButtonPosition.x,(resetButtonPosition.y - 1));
if(endingState == EndState.Crashed)
{
resetButton.gameObject.SetActive(true);
resetButton.transform.position = resetButtonPosition;
}
else if(endingState == EndState.Landed)
{
continueButton.gameObject.SetActive(true);
continueButton.transform.position = resetButtonPosition;
}
titleScreenButton.gameObject.SetActive(true);
titleScreenButton.transform.position = titleScreenButtonPosition;
}
else // If not, don't show the buttons
{
resetButton.gameObject.SetActive(false);
continueButton.gameObject.SetActive(false);
titleScreenButton.gameObject.SetActive(false);
}
The code for displaying the title screen button is the same; I don't understand why the Update() method works in one case but not in the other.
I have a pause button sprite that sets the paused flag to true:
void Update () {
coords = transform.position;
if (Input.touchCount == 1 && touched == false) // If the pause button is pressed
{
touched = true;
Vector3 wp = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
Vector2 touchPos = new Vector2(wp.x, wp.y);
if (collider2D == Physics2D.OverlapPoint(touchPos)) // pause and switch the unpause button in
{
unpauseButton.gameObject.SetActive(true);
unpauseButton.transform.position = coords;
character.paused = true;
}
}
if(Input.touchCount == 0)
touched = false;
}
character refers to the player character, and unpauseButton refers to the state of the pause button when the game is paused.
Could the title button be a child to another object that is not active when you pause ?
Looking at the Hierarchy window in Unity at design time, everything is at one level; no object is a child to any other object.
I'm still just as confused as ever about why this is going on. Any help would be greatly appreciated.
Your answer
Follow this Question
Related Questions
DontDestroyOnLoad not working 1 Answer
UnassignedReferenceException for assigned variable 1 Answer
Can you fix/update the old scripts at Unity.2019.1? 0 Answers
Problem with animation and void fixedupdate() 1 Answer
Asset Server problems. Old updates appearing in updates list after deleting assets 1 Answer