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
1
Question by gonzalitosk8 · Oct 04, 2013 at 02:18 AM · getcomponentanother script

enable/disable script from another script c#?

much say it's easy, but I do not understand achievement. unity will not let me compile and gives me an error .. this is the script I want to do. function it has is that the press of a button, x script is activated and another off

this is script :

 using UnityEngine;
 using System.Collections;
 
 public class nitro : MonoBehaviour {
     
     public Component N20;
     public Component Setup;
     
     void Start(){
     N20 = GetComponent<N20>();
     Setup = GetComponent<Setup>();
     }    
     
     void Update(){
     if(Input.GetButtonDown("nitro")){ 
     N20.enable = true;
     Setup.enable = false;    
     }
     else
     {
     if(Input.GetButtonUp("nitro")){ 
     N20.enable = false;
     Setup.enable = true;    
     }
    }        
   }
  }    

^^ thanks :)

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 Asritha · Jun 18, 2015 at 07:14 PM 0
Share

i have a doubt... if we disable the script, will all the methods used in it be disabled? Like all the coroutines too?

2 Replies

· Add your reply
  • Sort: 
avatar image
7
Wiki

Answer by Rahazan · Oct 04, 2013 at 02:25 AM

Instead of

     public Component N20;
     public Component Setup;

You could give these fields the type they are (which is probably what you wanted in the first place).

Here's how:

     public N20 n20;
     public Setup setup;

N20 is the class name (type), n20 is the name we are giving this variable.

And then you would use

     //.enabled instead of .enable
     n20.enabled = true;

to enable the script.

Note 1: You will also have to change your Start function (as the names of the fields have changed (N20 is now n20)).

Note2: By convention class names start with a capital letter, for better readability consider changing "public class nitro" into "public class Nitro".

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 gonzalitosk8 · Oct 04, 2013 at 02:51 AM 0
Share

THAN$$anonymous$$S! was so simple, now understanding the method, thank you very much!

avatar image Rahazan · Nov 13, 2013 at 04:21 AM 0
Share

Could you choose this answer as the accepted answer?

avatar image
3

Answer by aldonaletto · Oct 04, 2013 at 02:42 AM

You must declare a script as MonoBehaviour (base class) or as its actual type, which's just the script's file name without any extension - and never give to a variable a name equal to some existing type (this may cause incredible confusions!). Furthermore, the property that enables a script is enabled, not enable.

Supposing that the two scripts are called N20.cs and Setup.cs, for instance, you should write something like this:

 using UnityEngine;
 using System.Collections;
  
 public class nitro : MonoBehaviour {
  
   public N20 scriptN20;
   public Setup scriptSetup;
  
   void Start(){
     scriptN20 = GetComponent<N20>();
     scriptSetup = GetComponent<Setup>();
   }  
  
   void Update(){
     if(Input.GetButtonDown("nitro")){ 
       scriptN20.enabled = true;
       scriptSetup.enabled = false;  
     }
     else
     if(Input.GetButtonUp("nitro")){ 
       scriptN20.enabled = false;
       scriptSetup.enabled = true;   
     }
   }

 }
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 yami7 · May 16, 2014 at 09:29 PM 0
Share

great many thanks!

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

Multiple Cars not working 1 Answer

Multiplayer Script error 2 Answers

Inventory Script not working 1 Answer

C# GetComponent Issue 2 Answers

Spawn Script Not Working 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