• 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 safak93 · Mar 26, 2014 at 05:12 PM · ontriggerenterspeedincreasedecreaseboost

Increasing the speed for a few seconds doesn't work.

Hey guys.

If my player enter the trigger the speed should increase to 15 and after 10 seconds to 10 but with my script the speed goes to 10 instead of 15.

Here is the script:

 using UnityEngine;
 using System.Collections;
 
 public class SpeedPickup : MonoBehaviour {
 
     public string tag = "Player";
     public PlayerControllerScript controller;
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.gameObject.tag == tag)
         {
             controller.maxSpeed = 15f;
             Destroy(gameObject);
             speedTime();
             revertSpeed();
         }
     }
 
     IEnumerator speedTime ()
     {
         yield return new WaitForSeconds(10);
     }
 
     void revertSpeed ()
     {
         controller.maxSpeed = 10f;
     }
 }
Comment
Add comment · Show 1
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 trololo · Mar 26, 2014 at 09:37 PM 0
Share
 void OnTriggerEnter2D(Collider2D other)
 {
   if (other.gameObject.tag == tag)
   {
      controller.maxSpeed = 15f;
      StartCoroutine("speedTime");
      Destroy(gameObject); // other.gameObject no?
   }
 }
  
 IEnumerator speedTime ()
 {
     yield return new WaitForSeconds(10);
     revertSpeed();
 }
  
 void revertSpeed ()
 {
     controller.maxSpeed = 10f;
 }

1 Reply

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

Answer by Crazydadz · Mar 27, 2014 at 12:18 AM

If you destroy your gameObject before the end of the coroutine, the coroutine will stop. Put Destroy(gameObject) after the coroutine.

 void OnTriggerEnter2D(Collider2D other)
 {
   if (other.gameObject.tag == tag)
   {
      controller.maxSpeed = 15f;
      StartCoroutine(speedTime());
   }
 }
  
 IEnumerator speedTime ()
 {
     yield return new WaitForSeconds(10);
     revertSpeed();
     Destroy(gameObject);
 }
  
 void revertSpeed ()
 {
     controller.maxSpeed = 10f;
 }
Comment
Add comment · Show 6 · 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 safak93 · Mar 27, 2014 at 05:08 PM 0
Share

Thanks that works.

I have made it so. Will I get performance issues(for mobile device) with "GetComponent().enabled = false;"? Or is there a better way?

 void OnTriggerEnter2D(Collider2D other)
     {
         if (other.gameObject.tag == tag)
         {
             controller.maxSpeed = 15f;
             GetComponent<$$anonymous$$eshRenderer>().enabled = false;
             StartCoroutine(speedTime());
         }
     }
avatar image Crazydadz · Mar 27, 2014 at 05:19 PM 0
Share

GetComponent is quite heavy on $$anonymous$$obile. The best way is to cache it.

         private $$anonymous$$eshRenderer _mRenderer;
 
         private void Start ()
         {
                 _mRenderer = GetComponent<$$anonymous$$eshRenderer> ();
         }
 
         private void OnTriggerEnter2D (Collider2D col)
         {
                 _mRenderer.enabled = false;
         }
avatar image safak93 · Mar 27, 2014 at 05:48 PM 0
Share

Thanks you. :)

As last, if I try to acces another script with "gameObject.GetComponent().walkSpeed = 0.0f;". Is there also a better way for that?

avatar image Crazydadz · Mar 27, 2014 at 05:54 PM 0
Share

Do the same thing, cache the component on start. If the component is $$anonymous$$yOtherScript, just create a private field of it, allocated it in the Start(), use it in the OnTriggerEnter2D(...)

avatar image Crazydadz · Mar 27, 2014 at 06:00 PM 0
Share

In your case, you Destroy your gameObject and it is quite heavy on mobile. There is no need of caching component if you destroy your gameObject because when you recreate one, you ll need recall GetComponent....On mobile, try to disable gameObject and not destroy them. I don't know what you want to achieve with that script, but if there is multiple "Speed Trigger", search for "Object Pooling" on the web ins$$anonymous$$d of Destroying each trigger and Instantiating them again.

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

How to increase the speed of the character in OnTriggerEnter function? 1 Answer

Increase speed of object that enters OnTriggerEnter 1 Answer

Increasing speed of Character Motor on collision help 2 Answers

Increase sprite falling speed 1 Answer

Objects moving faster after time 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