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 clarioncat · Nov 08, 2013 at 12:04 PM · variablevaluestore

How to return to a value used before? (C#)

Hi. What I currently am trying to do is to take the player character's ability to move away from him during a dialogue.

For that, I wanted to reduce his speed to 0 as long as the dialogue menu is open. That in itself is no problem, but after the dialogue I dynamically want to change the speed back to the value it was before.

I guess that means I got to save that value before setting it to 0, then returning to that saved value. How do I do that? My example below does not work.

Thanks in advance!!

(It's about playerController.speed and usedSpeed).

 void OnGUI()
 {
     if (showDialogue)
     {
         GameObject Dusty = GameObject.Find("Dusty");
         PlayerController playerController = Dusty.GetComponent<PlayerController>();

         usedSpeed = playerController.speed;
         playerController.speed = 0;

         GUILayout.BeginArea(new Rect(10,10,200,200));
         GUILayout.Label("Stop running around, will you?");
         GUILayout.Label("I really feel nervous about this.");

         if (GUILayout.Button("Yeah, me too."))
         {
             Time.timeScale= 1;
             showDialogue = false;
             playerController.speed = usedSpeed;
         }

         if (GUILayout.Button("Really? I'm rather excited!"))
         {
             Time.timeScale= 1;
             showDialogue = false;
             playerController.speed = usedSpeed;
         }

         GUILayout.EndArea();
     }
Comment
Add comment · Show 2
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 vexe · Nov 08, 2013 at 12:11 PM 0
Share

This should work. But, could you give more details? what does changing the speed variable imply? How does your player move? is it a character controller, or...?

Not that it's related:

  1. If you're using usedSpeed only in this scope, then there's no need to declare it as a member variable - declare it locally: float usedSpeed = playerController.speed;

  2. No need for the two ifs, they already have the same body, just use an or (||):
    if (GUILayout.Button("stuff1") || GUILayout.Button("stuff2") // do your thing...

avatar image clarioncat · Nov 08, 2013 at 01:41 PM 0
Share

$$anonymous$$y character moves via a character controller, and speed is used there in: **Input.GetAxis("Horizontal") speed*

  1. playerController.speed is a variable from another script, if I don't refer to it the way I do the console says it doesn't know playerController in this context. Is there another way?

  2. you couldn't see that from my example, but the buttons will imply different consequences, therefore different bodies. Is there still a shorter way?

1 Reply

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

Answer by mattssonon · Nov 08, 2013 at 12:22 PM

You're setting usedSpeed to zero on the second iteration of the OnGUI loop. Let me explain:

First time showDialogue is true:

  1. usedSpeed is set to speed of playerController, e.g. 10.0f or whatever you have.

  2. playerController.speed is then set to 0.

Now, since this is running in the OnGUI loop, this code will run again if the player doesn't press anything, so the second time this happens:

  1. usedSpeed is set to speed of playerController, and now the playerController.speed is 0, because you just set it to 0 in the last iteration of OnGUI, so now usedSpeed is 0 as well.

What you could do to solve this is to only set usedSpeed when playerController.speed is not 0, so it remains the same during a dialogue scene, e.g.:

 if (showDialogue)
 {
    GameObject Dusty = GameObject.Find("Dusty");
    PlayerController playerController = Dusty.GetComponent<PlayerController>();
     
     if (playerController.speed != 0) {
         usedSpeed = playerController.speed;
     }
     
    playerController.speed = 0;
 
    GUILayout.BeginArea(new Rect(10,10,200,200));
    GUILayout.Label("Stop running around, will you?");
    GUILayout.Label("I really feel nervous about this.");
 
    if (GUILayout.Button("Yeah, me too."))
    {
      Time.timeScale= 1;
      showDialogue = false;
      playerController.speed = usedSpeed;
    }
 
    if (GUILayout.Button("Really? I'm rather excited!"))
    {
      Time.timeScale= 1;
      showDialogue = false;
      playerController.speed = usedSpeed;
    }
 
    GUILayout.EndArea();
 }
Comment
Add comment · Show 3 · 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 clarioncat · Nov 08, 2013 at 01:23 PM 0
Share

Hey $$anonymous$$att, thank you so much for your answer! Although your specific code example didn't work out for me (speed remains at 0), your hint regarding the OnGUI loop was essential for understanding where I was going wrong…

What I did now is storing the speed in usedSpeed via another function (SpeedCheck), with I call upon at the start at the script. That way, usedSpeed stores the playerController at the script's beginning.

I don't know if it's the most effective way, but it seems to work out fine… (would be perfect if speed could be checked and stored just at the start of OnGUI rather than at the start of the whole scene, but that's okay for now).

     void Start () {
         SpeedCheck ();
     }
     
     void Update () {
         Debug.Log("usedSpeed = " + usedSpeed);    
     }
     
     void SpeedCheck () {
     
         GameObject Dusty = GameObject.Find("Dusty");
         PlayerController playerController = Dusty.GetComponent<PlayerController>();
         usedSpeed = playerController.speed;
         
     }
     
     
     void OnGUI(){
         
         if (showDialogue){
             
             
             GameObject Dusty = GameObject.Find("Dusty");
             PlayerController playerController = Dusty.GetComponent<PlayerController>();
 
             playerController.speed = 0;
             
             GUILayout.BeginArea(new Rect(10,10,200,200));
             GUILayout.Label("Stop running around, will you?");
             GUILayout.Label("I really feel nervous about this.");
             
             if (GUILayout.Button("Yeah, me too.")){
                 Time.timeScale= 1;
                 showDialogue = false;
                 playerController.speed = usedSpeed;
             }
             if (GUILayout.Button("Really? I'm rather excited!"))
             {
                 Time.timeScale= 1;
                 showDialogue = false;
                 playerController.speed = usedSpeed;
             }
             
             
             GUILayout.EndArea();
             
             
         }
         
 
avatar image mattssonon · Nov 08, 2013 at 02:07 PM 0
Share

All right. You could use a boolean to only set the speed at the start of OnGUI, e.g.:

 if (!didSetSpeed) {
     // set speed
     didSetSpeed = true;
 }

Anyway, if your question is solved please either accept an answer or create one yourself and accept it, so the question gets closed. :)

avatar image clarioncat · Nov 08, 2013 at 04:02 PM 0
Share

Oh, this was my first question – didn't know it worked that way (: Done and done!

Thank you so much for your precious input.

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

18 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

Related Questions

assign value for all the script 2 Answers

Can I show a numeric variable from the slider on the GUI? 1 Answer

Variable value not changhing 3 Answers

Save Integer Values 1 Answer

Variable value not changing (bug or i'm just an idiot?) 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