• 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 FineGamer · Sep 18, 2014 at 05:12 PM · onoffscrpting

Turn off a script and then back on?

I need to turn off a script in my game and then turn it back on. Pretty much the point is here: I got ToggleCursor.cs I need to turn off MouseLook.cs Someone help me! :D

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 robertbu · Sep 18, 2014 at 05:47 PM 0
Share

"Did not work" means what? Did it generate an error? If you look in the inspector at the '$$anonymous$$ouseLook' component on the 'PlayerCamera', does the script get disabled? Do you have code that is reenabling this script that is perhaps getting executed? Any chance there are two $$anonymous$$ouseLook scripts on the camera? Etc. You need to dig deeper, because finding the component on the right object and setting the enabled flag will turn off the component.

3 Replies

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

Answer by robertbu · Sep 18, 2014 at 05:29 PM

You need to find the game object the script is on, either by tag, or by name, or by someone mechanism. Assuming it is on the camera:

   Camera.main.gameObject.GetComponent<MouseLook>().enabled = false;
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 robertbu · Sep 18, 2014 at 06:02 PM 1
Share

So what is the error? We cannot see your code, so we have to depend on you giving us enough information to figure out what is going on. The concept above is correct. You need to get the component from the specific game object that has the $$anonymous$$ouseLook script and set the enable flag. Assu$$anonymous$$g your camera has a unique name, you can try the alternate:

   GameObject.Find("NameOfCamera").GetComponent<$$anonymous$$ouseLook>().enabled = false; 
avatar image FineGamer · Sep 18, 2014 at 06:09 PM 0
Share

Thank you robertu soo much you saved me a loats of time just write a real comment not a reply so i can set it as solution! :D

avatar image
0

Answer by SnotE101 · Sep 18, 2014 at 05:26 PM

Create a new script and paste this in there.

bool disableScript; public scriptYouWantToStop scriptYouWantToStop;

bool disableScript; public scriptYouWantToStop scriptYouWantToStop;

 void Update
 {
     if(disableScript)
     {
         startCourtine(pauseScript());
     }
 }
 ienumberable pauseScript();
 {
     scriptYouWantToStop.enabled = false;
     yield return new waitforsecound(5);
 }

This is untested and probably has some spelling errors. Just drag the script you want to pause into the variable scriptYouWantToStop in the inspector. (It is easier if this script is on the same GameObject).

GoodLuck!

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 FineGamer · Sep 18, 2014 at 05:33 PM 0
Share

Yeah thanks but how do i start it up again, i need like a toggle type of a thing, the point here in the script is like you press e to toggle cursor/is it showing or not and then when i am showing the cursor i want to disable the script that allows looking around, and then re-enable it once i press/toggle e again.

avatar image fafase · Sep 18, 2014 at 05:39 PM 0
Share

$$anonymous$$ake sure to fix the errors before using this answer.

avatar image
0

Answer by zeeshandar · Sep 18, 2014 at 05:39 PM

 public GameObject scripttodisable = GameObject.Find("LevelNo").GetComponent<Yourscriptname>();
 scripttodisable.setactive(false);`

Or GameObject.Find("LevelNo").GetComponent().enabled=false;

LevelNo is the object which contains the script

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 fafase · Sep 18, 2014 at 05:42 PM 0
Share

Typos, typos, typos...

And make sure to put your code in code view or any less than greater than wont show.

avatar image FineGamer · Sep 18, 2014 at 05:56 PM 0
Share

I did the code and I got this: Assets/ToggleCursor.cs(6,75): error CS0029: Cannot implicitly convert type $$anonymous$$ouseLook' to UnityEngine.GameObject'

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Light script flashes on and off 1 Answer

Particle on and off 1 Answer

turn OnMouseDrag on and off 1 Answer

Show GUI texture then not 0 Answers

c# light off 2 Answers


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