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 Marco-I · May 12, 2015 at 05:25 PM · c#guiscriptingbasicscomponent

How to disable a custom script in a custom GameObject with C# ?

Hello! So, I have a Player GameObject, with a "FirstPersonController" script attached as component, ok?

Now, what I'm trying to do, is that when I enter into the Main Menu, the script simply turn off, so i'm unable to use "MouseLook" inside the "FirstPersonController" while i'm in the menu, got it?

Hope that is clear. I just tried the "Time.timeScale = 0;" statement, but it blocks only the movements. I need to turn off the script completely, and reactivate it when I restore the game.

Thank you.

PS= I use Unity5 and I'm not using JS.

Comment
Add comment · Show 2
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 Marco-I · May 12, 2015 at 07:13 PM 0
Share

Your comment is sooooo useful.......

avatar image xortrox · May 13, 2015 at 10:52 AM 0
Share

I'm not sure how this comment is not useful considering I provided you with the necessary features to do what you asked for? I also pointed out that it's pretty easy to figure this out by doing a little bit of google search, which in turn is also one of the requirements for posting a question on this website.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by DoTA_KAMIKADzE · May 12, 2015 at 05:29 PM

You have 3 general ways to enable/disable functionaliy of your script:

1)Add/Remove script when you need. I don't think that this is what you expect so I'll omit it.

2)Enable/Disable script.

     //when you need to disable:
     GetComponent<FirstPersonController>().enabled = false;

     //then when you need to enable:
     GetComponent<FirstPersonController>().enabled = true;

     //if you will do that from another script you'll need to get reference to your FirstPersonController script
     GameObject gObj = YourGameObjectThatHoldsFPC;
     gObj.GetComponent<FirstPersonController>().enabled = false;

Check out my answer THERE if you have no idea how to get the reference to your script.

3)Use some restriction variable, like for example boolean. This way only the part of code you don't need will not execute.

     //Just set it true false when you need and wrap your code that shouldn't execute in if(yourBool){}
     GetComponent<FirstPersonController>().yourBool = false;

You'll need to get the reference to script with this code as well if you'll change this bool from the other code.

Also note that you shouldn't write your code like that above^ it was just for example, as instead store reference to your script once and then use that variable, e.g.:

     FirstPersonController yourController;
     //then use it like:
     yourController.enabled = true;

Comment
Add comment · Show 4 · 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 Marco-I · May 12, 2015 at 07:12 PM 0
Share

I already tried a lot of times these statements, but they don't work. Unity says to me that can't find the "FirstPersonController" script.

I just found a temporary solution anyway, that works, that consist in adding these "" before and after the name of the script, and adding at the end "as behaviour".

Honestly I really don't know why your code doesn't work to me, i saw the same code in a lot of other websites.. bah! :(

avatar image DoTA_KAMIKADzE · May 12, 2015 at 10:31 PM 0
Share

Could you post the actual error along with script?

P.S. Specifically I'd like to see your FirstPersonController class declaration (e.g. the line of code that contains "class FirstPersonController") and the lines of code where you use GetComponent.

Also make sure that FirstPersonController is actually attached to object from which you try to get it (except if you utilize FindObject(s)OfType, in that case it would be pretty straight forward).

avatar image Marco-I · May 13, 2015 at 08:48 AM 0
Share

So, the declaration code of "FirstPersonController" is this one:

 **public class FirstPersonController : $$anonymous$$onoBehaviour
     { ... }**

The statement that I used (and that doesn't work for me) is this one:

 **GameOBJ.GetComponent<FirstPersonController>().enabled = false;**

And the error I get from the code below, is this one:

 **Assets/I$$anonymous$$7 Scripts/CentralTrigger.cs(32,46): error CS0246: The type or namespace name `FirstPersonController' could not be found. Are you missing a using directive or an assembly reference?**

$$anonymous$$y temporary solution that I found, is this one:

 (GameOBJ.GetComponent("FirstPersonController") as $$anonymous$$onoBehaviour).enabled = false;

Can you explain me why the first code doesn't work like the second? I can assure you that the script is correctly attached on the object, and has the checkbox.

avatar image DoTA_KAMIKADzE · May 13, 2015 at 11:04 AM 0
Share

Well for some reason Unity can't locate your FirstPersonController script.

$$anonymous$$ake sure that your FirstPersonController script is located inside Assets folder + that it is not hidden by Unity's magic folders + your folder containing this script should not start with dot (.folder).

Double check na$$anonymous$$g of FirstPersonController scrip and FirstPersonController class name and used FirstPersonController name inside the script where you call GetComponent - all of those should be with equal name.

avatar image
1

Answer by soapOD · Jan 19, 2017 at 10:35 PM

Just in case someone in the future needs an answer to this...

You need to add the appropriate namespace to reference the first person controller.

Add this to the top of your script:

 using UnityStandardAssets.Characters.FirstPerson;

then you can reference the first person controller by using: player.GetComponent<FirstPersonController>().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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

GUIText component won't render 1 Answer

Textures changing for no reason 0 Answers

How to loop an array of 2d sprites 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