- Home /
How To Reset Float From External Script (C#)
Hello all, I'm stuck on a problem regarding collecting items and applying speed from that item, right now I have this working, but every time I run the game, sometimes the speed will not reset back to it's original speed and keep going forwards. This is a 2D game for the record, forwards to me means to the right on the x axis.
I have multiple's of the same speed pick-up that the player needs to collect to stay going forwards, they are quite close together, they have to be for the game to work, each of them have the following script on them:
using UnityEngine;
using System.Collections;
public class SpeedItem : MonoBehaviour {
public float bonusTime = 0.3f;
public float bonusGain = 0.4f;
private bool collected = false;
private float timer = 0;
public float powerUpPosition = 2f;
void OnTriggerEnter2D(Collider2D other) {
if (other.gameObject.tag == "Player")
{
collected =true;
GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerMove>().forwardsSpeed+=bonusGain;
gameObject.renderer.enabled = false;
}
}
void Update()
{
if (collected == true)
{
timer += Time.deltaTime;
if(timer >= bonusTime)
{
GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerMove>().forwardsSpeed-=bonusGain;
Destroy(gameObject);
}
}
transform.position -= Vector3.right * powerUpPosition * Time.deltaTime;
}
}
And this is the player script it's referencing:
public float forwardsSpeed = 0.3f;
public float backwardsSpeed = 0.37f;
void Update() {
transform.position += Vector3.right * forwardsSpeed * Time.deltaTime;
transform.position -= Vector3.right * backwardsSpeed * Time.deltaTime;
}
}
I need to work out how to reset the forwardsSpeed back to it's original before it accepts another item, otherwise I get results like I am now which allows the player to gain more speed and only take away what it just recieved, and not all the amounts it received in that collection of items.
Is there anyone who knows how I can fix this and would be willing to help? I'd very much appreciate that, thanks!
its better you write coroutine Below given snippet may help you
IEnumerator (float WaitSeconds)
{
yield return new WaitForSeconds(WaitSeconds)
CurrentSpeed=DefaultSpeed;
}
//call the above method when ever you want to reset the speed to //default speed.
//Like Startcoroutine(5f);
//Hope this $$anonymous$$ay Help You
Thanks for the help sandeepsmartest, I didn't end up needing to use a coroutine for this particular case, I was just needing a bool to check when false for collection and then change the external float back to the original value using a different float.
Thank you for trying to help anyway though, I really appreciate that! - I've just posted my solution as an answer so the question can be marked as correct.
Answer by S_Byrnes · Jun 15, 2015 at 08:58 AM
Alright, so I worked this out on my own earlier, might as-well post what the answer was:
void OnTriggerEnter2D(Collider2D other) {
if (other.gameObject.tag == "Player")
{
collected = true;
GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerMove>().forwardsSpeed+=bonusGain;
gameObject.renderer.enabled = false;
}
if (other.gameObject.tag == "Bounds") {
Destroy (gameObject);
}
}
void Update()
{
if (collected == true) {
timer += Time.deltaTime;
if (timer >= bonusTime) {
GameObject.FindGameObjectWithTag ("Player").GetComponent<PlayerMove> ().forwardsSpeed = speedReset;
Destroy (gameObject);
collected = false;
}
if (collected == false){
GameObject.FindGameObjectWithTag ("Player").GetComponent<PlayerMove> ().forwardsSpeed = speedReset;
}
}
transform.position -= Vector3.right * fishSwim * Time.deltaTime;
}
}
What I did was added a speedReset float that was equal to my original speed in the other script, and referenced that variable in this script, using a bool that determined if it was no-longer being collected, and turning it false when the timer was over.
Your answer
![](https://koobas.hobune.stream/wayback/20220613201009im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Bullet not moving from script 3 Answers
Smoothed Rotation with 2D Top-Down View 1 Answer
2D Collect Item C# Increase Acceleration 2 Answers
C# 2D InfiniteRunner run with same speed even when going up 1 Answer
Multiple Cars not working 1 Answer