Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
10
Question by Chompski · Jan 18, 2012 at 10:44 PM · variabledisablevar

How do i disable a script from a different script?

Im just having issues setting it up as a var unless im doing it completely wrong which is likely

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

5 Replies

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

Answer by BiG · Jan 19, 2012 at 07:14 AM

Suppose that you are executing script A, and you want to disable script B. Both of them are attached to the same object. That's an easy task:

 GetComponent(B).enabled = false;
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 Chompski · Jan 19, 2012 at 03:15 PM 0
Share

They both have to be attached for this to work? i think thats what i did wrong :P

avatar image BiG · Jan 20, 2012 at 06:59 AM 2
Share

@Chompski, the code above works if both the scripts are attached to the same object.

However, if A and B are attached to different objects, you have to do this:

 GameObject.Find("other_object_name").GetComponent(B).enabled = false;
avatar image HsmBoy · Aug 14, 2012 at 01:31 PM 0
Share

hello ,I tried your method ,but error: 'eanbled' is not a member of 'UnityEngine.Component'

how can i fix it?thanks....

avatar image ScaleneParkour · Apr 10, 2015 at 05:06 AM 0
Share

THAN$$anonymous$$ YOU Big Jan I have been looking for hours now and now it works!!!!!!!!!!! THAN$$anonymous$$ YOU, I can finally leave the Insane Asylum :P

avatar image
15

Answer by Mander · Aug 14, 2012 at 02:19 PM

what you need to disable a script or a component from anyscript, is to find the gameObject where ur script is attached and then enable = false/true;

u can find ur game object by name or tag. store it in one variable and disable

for example C#:

 GameObject varGameObject = GameObject.Find("object");   or find with tag 
 
 GameObject varGameObject = GameObject.FindWithTag("Player"); then disable or enable script/component
 
 varGameObject.GetComponent<scriptname>().enabled = true;

example Js:

 var varGameObject = gameObject.Find("object")   Or with tag
 
 var varGameObject = gameObject.FindWithTag("player");
 
 varGameObject.GetComponent(scriptname).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
avatar image
3

Answer by kami1339 · Mar 28, 2019 at 12:28 PM

solved use this sample

Use this code

public GameObject otherobj;//your other object public string scr;// your secound script name void Start () { (otherobj. GetComponent(scr) as MonoBehaviour).enabled = false; }

it will work!!!!!

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 glenneroo · Apr 04, 2019 at 12:42 AM 1
Share

In the future, you should use the Code Sample button to format your code otherwise it's very hard to read:

 public GameObject otherobj; //your other object 
 public string scr;
 // your second script name 
 void Start () { 
    (otherobj.GetComponent(scr) as $$anonymous$$onoBehaviour).enabled = false; 
 } 
avatar image Rizing_Threat · Nov 06, 2020 at 03:51 PM 0
Share

Only workd if put in void start... i need it else where!

avatar image
2

Answer by mehedihasanp78 · Jun 24, 2020 at 02:44 PM

Assuming both script attach to same game object

 GetComponent<script name>().enabled = false;
Comment
Add comment · Show 1 · 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 MarshmelloStudios · Feb 16, 2021 at 09:29 PM 0
Share

this is amazing, best answer that worked for me

avatar image
0

Answer by youssefcraft · Aug 27, 2021 at 04:33 PM

 public ScriptName variableName;

ScriptName is the name of the script you want to access Variable name is just the name of the variable that contains the script you gotta assign it btw

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

18 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

Related Questions

enable disable variable of a script from another script 4 Answers

Changing variable from another script 1 Answer

How to modify variables from scripts in standard package? 2 Answers

how to disable script using OnColliderEnter/ OnTriggerEnter ? 1 Answer

Save what you write in textarea as variable? 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