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 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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

UI Button ON/Off 3 Answers

On/off switch from server to clients live 1 Answer

My night vision wont turn on/off 0 Answers

particle emitter on/off using the left mouse button 1 Answer

How to turn on a light by pressing and after few seconds turn it off 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