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 RichCoggin · Aug 16, 2013 at 02:10 PM · c#enable

Enable a script on object in c# help

Hi there,

I've looked around for ways to do this, but haven't found anything. I want to simply 'Enable' a script calling from a button press on another script. Once clicked, the script on the scene object is enabled. I have founds lots of .js examples. Do you have any examples of this in c#? Really stuck on this one.

Cheers

Rich

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

2 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by cmpgk1024 · Aug 16, 2013 at 02:15 PM

 YourGameObject.GetComponent(YourScript).enabled = false;

or use generics - you must add using System.Collections.Generic

 YourGameObject.GetComponent<YourScript>().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 SirDogey · Mar 07, 2017 at 02:04 AM 0
Share

What if your Script Name has a spaces? Such as First Person Controller?

avatar image Commoble SirDogey · Mar 07, 2017 at 05:37 AM 1
Share

Technically, you're using the name of the class here, not the name of the script file, and class names never have spaces in them.

avatar image
1

Answer by YoungDeveloper · Aug 16, 2013 at 02:17 PM

Hey, first you should getcomponent of that script.

 ScriptNameToDisable script; //creates that script data type
 
 script = GetComponent<ScriptNameToDisable>();
 
 script.enabled = false;
Comment
Add comment · Show 7 · 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 RichCoggin · Aug 16, 2013 at 02:25 PM 0
Share

Could you show me how this would look on the script? Not sure I understand. I have this button perfor$$anonymous$$g the action:

     if(GUI.Button(boxRect,"","Panel4Button")) {
 
 }

I guess I have to reference the scene object as well?

avatar image YoungDeveloper · Aug 16, 2013 at 02:36 PM 0
Share
 if(GUI.Button(boxRect,"","Panel4Button")) { //if your button is corrent
      ScriptNameToDisable script; 
      
     script = GetComponent<ScriptNameToDisable>();
      
     script.enabled = false;
 }

You can get component on the go (in this function only when called), and after the function has finished, that data type will be deleted, which will free memory allocation.

avatar image RichCoggin · Aug 16, 2013 at 02:52 PM 0
Share

Doesn't seem to work. I've added the code into the script, but nothing happens. Is this right?

                 TargetLostScript script; ///name of the script on the object
  
             script = GetComponent<TargetLostScript>(); //name of the script
  
             script.enabled = true; //this turns on the script

Thanks for your help.

avatar image RichCoggin · Aug 16, 2013 at 02:58 PM 0
Share

Di I need to be referencing the Object that the script is on?

avatar image YoungDeveloper · Aug 16, 2013 at 03:06 PM 0
Share

Oh, sorry i made a mistake. We need to find the gameobject name first to call it.

gameObject.GetComponent();

But the point here is, how do you want to get that object. Easy way would be attaching that script gameobject in a public variable;

So the final script:

 public GameObject object; //drag the gameobject which have that script you want to disable, in the inspector.
 
     if(GUI.Button(boxRect,"","Panel4Button")) { //or any if check
     ScriptNameToDisable script;
      
     script = object.GetComponent<ScriptNameToDisable>();
      
     script.enabled = false;
     }

Sorry for the mistake again.

Show more comments

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

19 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

Related Questions

Enable script (get "script" number?) 2 Answers

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

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

The name `UnityEditor' does not exist in the current context 3 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