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 mian_24 · Jan 25, 2014 at 01:58 AM · disable

disable a script c#

hi. i am currently trying to disable a script but it turns out that the code that i am using is not working at all.i got this code from different forums and the code that they suggest is similar to each other and they claim that it is 100% accurate and working.

this is the code

 public ShowTheDetails showDetails;
 showDetails = GameObject.FindGameObjectWithTag("detailButton").GetComponent();
 showDetails.enabled=false;

the purpose of this code to my game is to disable a specific gameObject by disabling the script that is attached into it..can you please help me solve this problem???By the way this is a c# code.

Thank you in advance.

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
1

Answer by getyour411 · Jan 25, 2014 at 02:03 AM

Put the name of your script inside the GetComponent, or drag/drop the name of the target script into showDetails in the Editor (since you made it public) in which case you don't need the Find

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
avatar image
1

Answer by RecursiveRuby · Jan 25, 2014 at 03:40 AM

Are you sure it is finding the object properly? As getyour411 said you could just drag and drop it. Also do you want to just disable the script or the gameobject? perhaps you may want to try

 showDetails.gameObject.SetActive(false);
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 mian_24 · Jan 25, 2014 at 02:00 PM 0
Share

i am sure that it finds the exact script because i have tried to call a method from that script in order to know if it is accessing the desired script. but when i tried to disable it, it doesn't work.

avatar image RecursiveRuby · Jan 25, 2014 at 06:23 PM 3
Share

I see you're not specifying a type in GetComponent(), does this even compile? or is this not the code you are actually using? If I wanted to disable the script Foo I would do

 GameObject.FindGameObjectWithTag("detailButton").GetComponent<Foo>().enabled=false;

and if I wanted to disable the object itself I would do

 GameObject.FindGameObjectWithTag("detailButton").SetActive(false);

I would be surprised if either of these examples don't do what you want. $$anonymous$$eeping in $$anonymous$$d that the former only disables the script and the latter will disable the entire object completely.

avatar image 686InSomNia686 RecursiveRuby · Jan 04, 2016 at 03:37 PM 0
Share

GameObject.FindGameObjectWithTag("$$anonymous$$ainCamera").GetComponent().enabled = false;

It works perfectly for me, Thanks Barabicus and happy new year!

avatar image
0

Answer by iwaldrop · Jan 25, 2014 at 08:06 PM

That can't be the full code, because the first line declares a member variable while lines 2 and 3 assign values to that variable outside of a method body. Something is missing from either your paste or from your source.

It can eaisly be fixed by doing this:

 public ShowTheDetails showDetails;
 
 void Awake()
 {
     showDetails = GameObject.FindGameObjectWithTag("detailButton").GetComponent<ShowTheDetails>(); 
     showDetails.enabled=false;
 }

I'm not saying that it will work right out of the gate, because I don't have any visibility over your codebase, and whether the ShowTheDetails object is what you're looking for, or if it is a property or variable of another script that you'd be using GetComponent for.

If not, please consider adding more detail to your questions in the future.

Also, if you want to disable the GameObject, you have to do it a different way. Disabling a script doesn't disable a gameobject, but the following will:

     showDetails.gameObject.SetActive(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 mian_24 · Jan 26, 2014 at 12:32 AM 0
Share

I have tried that SetActive(false) but it makes my game object invisible in my screen.All i want is to disable a script so that the functions attached to the game object would be disable but the game object is still visible in my screen.

avatar image iwaldrop · Jan 26, 2014 at 01:22 AM 0
Share

Then word your question better. You said, "the purpose of this code to my game is to disable a specific gameObject by disabling the script that is attached into it..can you please help me solve this problem???By the way this is a c# code."

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

21 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Disable Movement script on trigger enter 1 Answer

Material doesn't have a color property '_Color' 4 Answers

[Solved]cant disable/enable scripts 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