- Home /
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();
}
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:
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;
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...
$$anonymous$$y character moves via a character controller, and speed is used there in: **Input.GetAxis("Horizontal") speed*
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?
you couldn't see that from my example, but the buttons will imply different consequences, therefore different bodies. Is there still a shorter way?
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:
usedSpeed
is set to speed ofplayerController
, e.g. 10.0f or whatever you have.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:
usedSpeed
is set to speed ofplayerController
, and now theplayerController.speed
is 0, because you just set it to 0 in the last iteration ofOnGUI
, so nowusedSpeed
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();
}
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();
}
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. :)
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
Follow this Question
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