Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Sep 06, 2016 at 11:14 AM by Zythus for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Zythus · Dec 15, 2015 at 04:26 PM · unity 5coroutinecountdowncount

Getting variable from script while keeping other code from it

Hello everyone, in my GameManager.cs I was trying to do a coroutine which stops the scene, counts for 3 seconds, and starts everything in the scene.

I managed to stop game timer and stuff, but I've got a problem with Player not being stopped by the coroutine.

Player is managed by Player.cs. It has speed variable and some code, for example losing speed while gaining altitude.

I would like to keep that code, but stop Player from moving for 3 seconds while coroutine works, and then let him start.

This is what I tried to do in my GameManager. I get the component from other script and the speed variable. Bool 'count' checks if the coroutine is working. While it is, Player's speed is meant to be 0, while it is not, speed is meant to be 90, and timer is supposed to start.

 void Update()
     {
         GameObject plane = GameObject.Find("Player");
         Player s = plane.GetComponent<Player>();
          
         //Countdown working
         if (count == true)
         {
             s.speed = 0f;
             TimeText.text = ("0.0");
         }
       
         //Countdown finished
         if (count == false)
         {
             s.speed = 90.0f;
             //Game timer
             time += Time.deltaTime;
             TimeText.text = (time.ToString("f1"));
         }

Timer starts flawlessly, but I can rotate the Player while coroutine is playing (which I would like to avoid), and when the Player starts moving it doesn't preserve the code from Player.cs (losing speed while gaining altitude stuff).

All I want is stop any Player movements for 3 seconds, and then let him go with all the code in Player.cs.

How can I tackle this?

Comment
Add comment
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

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by OncaLupe · Dec 17, 2015 at 10:39 AM

I'd have a bool in the Player script, that if set to true you just ignore all movement inputs and not calculate any position changes. The bool should be set and cleared by functions that get called by the GameManager as needed. If you use physics, can also set isKinematic to true on pause, and false on unpause.

 public class Player : MonoBehaviour
 {
     bool isPaused = false;
 
     void Update()
     {
         //Anything that needs to be done even if paused goes here
 
         if(isPaused)
             return;
         
         //Do normal game stuff (inputs, movement, etc)
     }
 
     public void PauseMovement()
     {
         isPaused = true;
 
         //Anything else you need to stop movement
     }
 
     public void UnpauseMovement()
     {
         isPaused = false;
 
         //Anything else needed after unfreezing
     }
 }



 public class GameManager : MonoBehaviour
 {
     Player playerScript;
 
     void Start()
     {
         //Finds and GetComponents should be done once and stored as they're both slow operations
         playerScript = GameObject.Find("Player").GetComponent<Player>();
     }
 
     IEnumerator FreezeGame3Seconds()
     {
         playerScript.PauseMovement();
 
         //Do freeze game stuff
 
         yield return new WaitForSeconds(3f);
 
         //Do unfreeze stuff
 
         playerScript.UnpauseMovement();
     }
 }
Comment
Add comment · Show 1 · Share
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 Zythus · Dec 22, 2015 at 01:52 PM 0
Share

Thanks for the idea. I put playerScript in the Start(), but really all I had to do was to set the speed in FixedUpdate for the count and set it at the end of my countdown coroutine and it worked flawlessly. Again complicating simple things for myself.

Follow this Question

Answers Answers and Comments

42 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 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 avatar image avatar image avatar image avatar image

Related Questions

Trying to move buttons via Coroutine and transform.Translate 0 Answers

Testing Duration With Unit Tests 0 Answers

display rotating animation while calculating operation that takes long time 0 Answers

Coroutine [sometimes] stops working 1 Answer

unity 5 stopping 0 Answers


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