- Home /
GetKey/GetKeyDown, Paired with Adjusting Stats
I am using GetKey and GetKeyDown to cast a spell, and then adjust my Magic Power in text on the screen.
The only problem is, the Text does change, however only as the key is pressed down then reverts to my default text. How do I get my text to stick with the adjustments?
Answer by AgentFox101 · May 19, 2020 at 07:40 PM
If you're updating the text in your update method, it will change it back as soon as the key is no longer being pressed, an example of this would be like:
void Update(){
myText.text = "normal text";
if(Input.GetKey(KeyCode.E)){
myText.text = "pressed text";
}
}
If you want to prevent this, only update the text when a key is pressed, and I suggest you set it in the start method so it isn't empty to begin with.
void Start(){
myText.text = "normal text";
}
void Update(){
if(Input.GetKey(KeyCode.E)){
myText.text = "pressed text";
}
}
Edit: sorry for the bad code formatting lol.
void Start() { screenStats = GetComponent (); $$anonymous$$Pactual = screenStats.playermpactual; projectileplace = transform; }
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.J))
{
Debug.Log ("Spell Cast");
throwprojectile ();
}
}
public void throwprojectile()
{
GameObject projectile = (GameObject) Instantiate(projectileobject, projectileplace.TransformPoint (0, 20, 10), projectileplace.rotation * Quaternion.Euler (-90f, 0f, 0f));
projectile.GetComponent<Rigidbody> ().AddForce (projectileplace.forward * force, Force$$anonymous$$ode.Impulse);
Destroy (projectile, 3);
$$anonymous$$Pactual = $$anonymous$$Pactual - 8;
}
This is a script I have for casting a spell; it calls the script that set up the screen statistics. so, at the bottom, $$anonymous$$PActual changes when the spell is cast
and weirdly enough, when i set the original script to update the stats, the spell will no longer cast.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to get holding key to prioritize 1 Answer
Check if key is pressed once 1 Answer
Text in the scene is not being updated! 0 Answers