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 /
avatar image
0
Question by Spider_newgent · Aug 01, 2016 at 06:05 PM · enumstate-machinegetkeydownswitch-case

change object state using enum switch from another script

I've searched extensively and either I'm not seeing/getting it, or the question isn't answered.

I have simple Script to control the player, and use an enum to control the characters state:

  public void SetState(pState p) {
         playerState = p;
         r = GetComponent<Rigidbody>();
 
         switch (playerState) {
 
             case pState.ascend:
                 Debug.Log("Flap");
                 r.AddForce(Vector3.up * 10, ForceMode.Impulse);
                 playerState = pState.descend;
                 break;
 
             case pState.descend:
                 Debug.Log("falling");
             break;
 
             case pState.dead:
                 Debug.Log("dead");
             break;
 
         }
     }

Another script handles the input and when space is pressed should change the players state, like so:

 using UnityEngine;
 using System.Collections;
 
 public class UserInput : MonoBehaviour {
 
     public GameObject p;
     public PlayerScript ps;
 
     void Start () {
         ps = p.GetComponent<PlayerScript>(); 
     }
     
     void Update () {
 
         if (Input.GetKeyDown(KeyCode.Space)) {
             ps.playerState = pState.ascend;
         }
     }
 }

Please can someone explain what I'm doing wrong/why this doesn't work? Bonus points if you can guess what game I'm emulating.

Thanks

Dan

Comment
Add comment · Show 8
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 Hanoble · Aug 01, 2016 at 06:11 PM 1
Share

You are setting the pState.ascend in your UserInput class, but why are you just not using the public method of SetState() in the PlayerControl class? Without running through the switch logic you are not seeing the force being added, which probably makes you think the state is not being switched. Once you get the component and space is pressed in the UserInput class, call ps.SetState(pState.ascend).

The SetState() then handles the updating of the state for you, as well as the state specific settings.

Also....Flappy Bird....

avatar image Spider_newgent Hanoble · Aug 01, 2016 at 06:19 PM 0
Share

Thanks Hanoble.

I tried that initially and it still doens't work.

 public void SetState(pState p) {
         playerState = p;
         r = GetComponent<Rigidbody>();
 
         switch (playerState) {
 
             case pState.ascend:
                 Debug.Log("Flap");
                 r.AddForce(Vector3.up * 10, Force$$anonymous$$ode.Impulse);
                 playerState = pState.descend;
                 break;
 
             case pState.descend:
                 Debug.Log("falling");
             break;
 
             case pState.dead:
                 Debug.Log("dead");
             break;
         }
     }

and then in UserInput:

 if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)) {
             ps.SetState(pState.ascend);
         }

This does print "flap" but why doesn't it addForce to the player?

Oh, and you're right. 10 extra points for you!

avatar image Hanoble · Aug 01, 2016 at 07:45 PM 0
Share

If it does print "flap", then you are getting into that section of code. The next step is to debug the rigidbody and make sure the force is being added. The RB is not set to kinematic correct? There is a RB on there as well correct? Both simple things, but we have all overlooked these simple things in the past and spent waaayyyy too much time troubleshooting obvious fixes.

Run back through the setup of the RB and make sure everything is correct. If it is still not working, attach visual studio to the Unity instance and place a breakpoint at the AddForce line and try to further breakdown what is going on.

avatar image Spider_newgent Hanoble · Aug 01, 2016 at 09:14 PM 0
Share

The rigidbody is set up correctly. If I set the pState to ascend in the inspector at run time he recieves the impulse with no problem. If I set playerState = pState.ascend; in the Start() of the PlayerScript, it works fine. It's only when being set from another script. I'm not sure how to use a breakpoint but did take a look at them. $$anonymous$$y scripts now look like this:

PlayerScript

  using UnityEngine;
  using System.Collections;
  
  public enum pState { ascend, descend, dead };
  
  public class PlayerScript : $$anonymous$$onoBehaviour {
  
      public pState playerState;
      public Rigidbody r;  
  
      void Start () {
          // playerState = pState.descend;
          playerState = pState.ascend;
      }
      
      void FixedUpdate () {
          SetState(playerState);
          Debug.Log("state is: " + playerState);
      }
  
      public pState SetState(pState p) {
          playerState = p;
          r = GetComponent<Rigidbody>();
  
          switch (playerState) {
  
              case pState.ascend:
                  Debug.Log("Flap");
                  r.AddForce(Vector3.up * 10, Force$$anonymous$$ode.Impulse);
                  playerState = pState.descend;
                  break;
  
              case pState.descend:
                  Debug.Log("falling");
              break;
  
              case pState.dead:
                  Debug.Log("dead");
              break;
          }
          return playerState;
      }
  }

UserInput

  using UnityEngine;
  using System.Collections;
  
  public class UserInput : $$anonymous$$onoBehaviour {
  
      public GameObject p;
      public PlayerScript ps;
  
      void Start () {
          ps = p.GetComponent<PlayerScript>(); 
      }
      
      void Update () {
  
          if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)) {
              ps.SetState(pState.ascend);
          }
      }
  }

It doesn't make a difference whether I return the playerState in setState or just have it return void.

Thanks again.

Dan

avatar image vintar · Aug 01, 2016 at 09:06 PM 0
Share

Can you tell me why you call SetState() in FixedUpdate() ?

avatar image Spider_newgent vintar · Aug 01, 2016 at 09:14 PM 0
Share

Only reason it's called in FixedUpdate() is because I'm using addForce(). Force of habit to use FixedUpdate when changing velocity etc. Calling in Update() yields the same issue. Thanks.

avatar image Hanoble · Aug 01, 2016 at 10:01 PM 0
Share

I just plugged in your scripts to a project and it does work. I created a new scene, put both scripts on a cube, drug in the cube and scripts to the inspector as needed, hit play and hit space bar to get my cube co$$anonymous$$g back up. As the cube gains more momentum going down it takes more button presses to bring it back up, and the space presses continue to add force, so if I simply mash space over and over I can get the cube to continue to climb for several seconds after I stop pressing space. That is probably not the exact mechanic you are looking for, but that is more tweaks than it is fixing why it does not work.

I am not sure as to exactly what is not working on your end, but the scripts do work. I would also suggest removing the SetState() in the FixedUpdate(), as there is no reason to set the state every frame.

avatar image Bunny83 · Aug 01, 2016 at 10:58 PM 0
Share

moved to the help room

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by dead_byte_dawn · Jun 09, 2017 at 12:40 AM

Robert Miles has a pretty good chapter on Objects, Structures and References. I'd start here: http://www.csharpcourse.com/

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

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

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

Related Questions

How to check to see if Switch Case is true. 1 Answer

GetKeyUp does not get called sometimes. 1 Answer

moving object up & down with 1 keycode. 0 Answers

Several instances destroyed when one is shot by player 0 Answers

Parse string value to add to enum List 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