Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Creative Inventory · Nov 08, 2015 at 03:50 PM · script.disable

Disable a script for a few seconds

can somebody show me how to disable this script for a few seconds once it collides with an object tag "bouncy object" here's the script:

 using UnityEngine;

 using System.Collections;

  public class playermovement : MonoBehaviour {
 
 public float speed = 15f;
 private Vector3 target;

 void Start () {
     target = transform.position;
 }
 
 void Update () {
     if (Input.GetMouseButtonDown (0)) {
         target = Camera.main.ScreenToWorldPoint (Input.mousePosition);
         target.z = transform.position.z;
     }        
         transform.position = Vector3.MoveTowards (transform.position, target, speed * Time.deltaTime);
         }
 
    }

Thank you!!

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Cepheid · Nov 09, 2015 at 12:29 AM

Try having a look at Coroutines and the WaitForSeconds method provided by Unity.

Coroutine Scripting Reference

You could use this to call a method on another script which contains an instance of the playermovement script so that it disables it, waits for a specified amount of time and then re-activates the script through the enabled variable. For example have a look below:

     public playermovement playerMovementRef;
 
     void Start ()
     {
         StartCoroutine("DisableScript");
     }
 
     IEnumerator DisableScript ()
     {
         playerMovementRef.enabled = false;
 
         yield return new WaitForSeconds(3f);
 
         playerMovementRef.enabled = true;
     }


Comment
Add comment · Show 1 · 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 Creative Inventory · Nov 09, 2015 at 08:11 PM 0
Share

Thank you, I've tried the script out and it kinda works, the only problem is that when my player collides with an object that bounces it, the player shakes a lot, also when the player moves left it's movement is all weird, until it collides with another box collider. How would I stop this from occurring? Once again thanks in advances. @Cepheid

avatar image
0

Answer by Statement · Nov 09, 2015 at 08:25 PM

 void OnCollisionEnter(Collision collision)
 {
    float fewSeconds = 3;
    if (collision.gameObject.tag == "bouncy object")
       Disable(fewSeconds);
 }
 
 void Disable(float time)
 {
     enabled = false;
     // If we were called multiple times, reset timer.
     CancelInvoke("Enable"); 
     // Note: Even if we have disabled the script, Invoke will still run.
     Invoke("Enable", time);
 }
 
 void Enable()
 {
     enabled = true;
 }
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

33 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 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 enable and disable a script on an game object by pressing a keyboard button? 1 Answer

How to disable my buttons when i press and enable the other ones? 0 Answers

How do I disable enemy ai the script after enemy ai dies 1 Answer

Identical Lines of Code Will Either Disable, or Wont Disable a Script. Need Help. 0 Answers

What is wrong with my script? 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