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 gianni123 · May 21, 2010 at 04:17 PM · stop

how to stop a script for a while?

how to stop a script for a while? I wish that when the player passes a trigger, a script is blocked for a few seconds,but doesn't work

var timeOut = 3.0;

function Awake () { Invoke ("Enable", timeOut); }

function OnTriggerEnter (collision : Collider) {

if (collision.gameObject.tag == "Player") { GameObject.Find("messoplayer").GetComponent("input").enabled=false; GameObject.Find("Player").GetComponent("FPSWalker").enabled=false; GameObject.Find("Player").GetComponent("MouseLook").enabled=false; } }

function Enable () { GameObject.Find("messoplayer").GetComponent("input").enabled=true; GameObject.Find("Player").GetComponent("FPSWalker").enabled=true; GameObject.Find("Player").GetComponent("MouseLook").enabled=true; }

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

3 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by equalsequals · May 21, 2010 at 05:26 PM

Whenever I see a question involving "do x for t seconds" I always suggest using the MonoBehaviour Coroutine.

Do something like this:

void OnTriggerEnter(Collider other)
{
    if(other.gameObject.name != "Player") return; //skips the rest of the function if something else entered the trigger besides what we want to actually trigger the event
    StartCoroutine(MyCoroutine());
}
IEnumerator MyCoroutine()
{
    //disable the desired script here
    yield return new WaitForSeconds(10F);
    //enable it here
}

That code will start a Coroutine when our Player enters the trigger. The Coroutine will then do something, wait 10 secs and do something else. Should get you the desired result.

Cheers,

==

p.s. I code in C#, obviously. If you need Javascript you could compare my script to the docs on Coroutines to port it over.

Comment
Add comment · Show 8 · 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 gianni123 · May 21, 2010 at 06:01 PM 0
Share

thank you.. I'm trying to change the script "acrivate trigger to activate it after X seconds", without success .. Here's the script

http://forum.unity3d.com/viewtopic.php?p=322080#322080

avatar image equalsequals · May 22, 2010 at 04:28 PM 0
Share

That script is probably a little overly complicated for what you most likely need. Judging by your request in your question, you have an FPSWalker script that when the object that the script is attached to enters a trigger, the script is temporarily disabled then re-enabled. Am I correct in my assumption?

avatar image gianni123 · May 24, 2010 at 12:56 PM 0
Share

exactly!you have a better script?

avatar image gianni123 · May 24, 2010 at 02:38 PM 0
Share

I made this script .. now when I pass the trigger scripts are disabled, but then you do not enable more

avatar image gianni123 · May 24, 2010 at 03:24 PM 0
Share

posted! .

Show more comments
avatar image
0

Answer by Random Indie · May 21, 2010 at 05:15 PM

One way you could do it would be to either disabable the component by setting enable to false. You might have to do that from another script though as I'm not sure you can get a disabled script to re-enable itself. You can use the getcomponent function to obtain a reference to the necessary script.

You could also try encapsulating the code you want disabled in a big

if(!disabled) {...}

clause. You'd set the disabled flag in the trigger collision code given your example.

Hope that helps.

Comment
Add comment · Show 2 · 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 equalsequals · May 24, 2010 at 03:29 PM 0
Share

No, you can't get a disabled script to enable itself.

avatar image Random Indie · May 24, 2010 at 08:47 PM 0
Share

That's what I figured, wouldn't really be disabled otherwise :)

Thanks for confirmation, have't had time to make sure.

avatar image
0

Answer by gianni123 · May 24, 2010 at 03:23 PM

disable script(WORK):

function OnTriggerEnter (collision : Collider) {

if (collision.gameObject.tag == "Player") { GameObject.Find("messoplayer").GetComponent("input").enabled=false; GameObject.Find("Player").GetComponent("FPSWalker").enabled=false; GameObject.Find("Player").GetComponent("MouseLook").enabled=false; print("HELLO"); } }

re-enable script(DOESN'T WORK)

var timeOut = 2.0; function OnTriggerEnter (collision : Collider) { if (collision.gameObject.tag == "Player") { print("hello1"); Invoke ("Activate", timeOut); } }

function Activate () {
GameObject.Find("messoplayer").GetComponent("input").enabled=true; GameObject.Find("Player").GetComponent("FPSWalker").enabled=true; GameObject.Find("Player").GetComponent("MouseLook").enabled=true; print("PROVA3"); }

Comment
Add comment · Show 3 · 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 equalsequals · May 24, 2010 at 09:56 PM 0
Share

Are you deactivating the script in which this all takes place in?

avatar image gianni123 · May 25, 2010 at 07:31 AM 0
Share

I disabled the script and it works. but the script that should resume does not work

avatar image equalsequals · May 28, 2010 at 06:15 PM 0
Share

I see you are using 2 scripts. You really do not need to. In your disable script paste your entire Activate function from the enable script and at the end of OnTriggerEnter in your disable script use the call to Invoke.

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

No one has followed this question yet.

Related Questions

How can I completely stop a script? 1 Answer

Stop running script when object is selected. 1 Answer

Timer that stops at the end of the game 3 Answers

How to stop accelerometer when my object touches the plane/ground? 1 Answer

Stop momentum of a ball 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