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 S_Byrnes · Jun 15, 2015 at 01:19 AM · c#2dspeedreset

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!

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 sandeepsmartest · Jun 15, 2015 at 06:54 AM 0
Share

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

avatar image S_Byrnes · Jun 15, 2015 at 09:01 AM 0
Share

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.

1 Reply

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

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.

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

22 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

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


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