• 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 Pixeltoro · Jun 06, 2016 at 07:14 AM · countdownstate machine

state machine countdown timer

¡Hola! I'm still learning so please bear with me.

i recently watched the live training for state machines link: http://unity3d.com/learn/tutorials/topics/scripting/using-interfaces-make-state-machine-ai

i am trying to modify this code for my own purposes but have come across a stumbling block. i have tried to make a simple countdown time to display using the new UI system but when i play all it displays is the original timeRemaing. for some reason it just doesn't seem to count down can anyone help? here is my code so far.

public class StatePatternGameplay : MonoBehaviour {

 [HideInInspector] public IGameplayState currentState;
 [HideInInspector] public CountdownState countdownState;


 public Text countdownText;
 public float timeRemaining = 5f;

 private void Awake()
 {
     countdownState = new CountdownState (this);
 }
     
 void Start () 
 {
     currentState = countdownState;
 }
     
 void Update () 
 {
     currentState.UpdateState ();
 }

and

public class CountdownState : IGameplayState {

 private readonly StatePatternGameplay gameplay;

 public CountdownState (StatePatternGameplay statePatternGameplay)
 {
     gameplay = statePatternGameplay;
 }

 public void UpdateState()
 {
     gameplay.timeRemaining -= Time.deltaTime;

     gameplay.countdownText.text = Mathf.Round (gameplay.timeRemaining).ToString();
 }
     

 public void ToCountdownState()
 {
     Debug.Log ("Can't transition to same state");
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by ericbegue · Jun 06, 2016 at 01:18 PM

Hi,

Do you have only on state defined? Is currentState assigned to countdownState and stay so?

Otherwise, I don't see why this code is not working.

Finite State Machine is a very common tool. Despite that it is simply to understand and can become cumbersome to use.

I suggest to learn about Behaviour Tree. It's a good alternative to FSM and is easier to use on the long run.

Have a look at Panda BT (www.pandabehaviour.com), it's a script-based Behaviour Tree framework.

If you have any question about using this tool, you're welcome on this thread.

Comment
Add comment · Show 11 · 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 Pixeltoro · Jun 06, 2016 at 03:06 PM 0
Share

Thanks for the reply, I had other states defined but commented them out as they weren't in use yet.

CountdownState is the current state.

I have no experience with behaviour trees so may need to look into those as never used before

avatar image ericbegue Pixeltoro · Jun 06, 2016 at 03:15 PM 0
Share

You could add a Debug.Log in UpdateState to test whether this function get called.

Behaviour Tree is really worth learning. It's more intuitive than FS$$anonymous$$ when it comes to define logics involving ti$$anonymous$$g. If you have any question about it, I'd be glad to help.

avatar image Pixeltoro ericbegue · Jun 06, 2016 at 06:17 PM 0
Share

so i tried the debug.log and nothing happened. any other ideas?

Show more comments
avatar image
0

Answer by Pixeltoro · Jun 11, 2016 at 12:47 PM

After much pain of trying to figure out the issue I decided to use this state machine and just import each of the scripts into the new state machine testing each stage

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 ericbegue · Jun 11, 2016 at 12:55 PM 0
Share

What are you reffering to by "this state machine"?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Countdown code not working 1 Answer

How to make a coundown of destroyed objects ? 1 Answer

3D number countdown 0 Answers

how to make a varied countdown timer 1 Answer

Time goes to fast for the code. (C#) 2 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