Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by CrossRiverGames · Jan 16, 2014 at 03:55 PM · updateunity4.3not-workingconfused

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.

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JustAnotherDude · Jan 16, 2014 at 04:07 PM 1
Share

How are you pausing the game exactly ?

avatar image CrossRiverGames · Jan 16, 2014 at 04:12 PM 0
Share

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.

avatar image JustAnotherDude · Jan 16, 2014 at 04:32 PM 1
Share

Could the title button be a child to another object that is not active when you pause ?

avatar image CrossRiverGames · Jan 16, 2014 at 04:38 PM 0
Share

Looking at the Hierarchy window in Unity at design time, everything is at one level; no object is a child to any other object.

avatar image CrossRiverGames · Jan 20, 2014 at 08:43 PM 0
Share

I'm still just as confused as ever about why this is going on. Any help would be greatly appreciated.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges