• 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
Question by SMASapps · Jan 06, 2013 at 09:41 AM · changedisableswitchenable

switch scripts?

is this right to switch scripts?

 var Script1 : GameObject;
 var Script2 : GameObject;
 private var startObject : int = 1;
 
 function Start ()
 {
     Script1.enabled = true;
     Script2.enabled = false;
     startObject = 1;
 }
 
 function Update ()
 {
     if(Input.GetKeyDown("f") && (startObject == 1))
     {
         startObject = 2;
         Script1.enabled = false;
         Script2.enabled = true;
     }
     else if (Input.GetKeyDown("c") && (startObject == 2))
     {
         startObject = 1;
         Script1.enabled = true;
         Script2.enabled = false;
     }
 }

is this right to switch scripts?`

Comment

People who like this

0 Show 3
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 AlucardJay · Jan 08, 2013 at 11:07 AM 1
Share

for any help, please format your code. You can do this by highlighting all your code, then clicking the 10101 button at the top of the edit window.

Though, it may be easier just to delete the code, paste it in again from the source, then highlight all the code and press the 101010 button at the top of the edit window before posting. When you don't mark your code as code, the text gets html escaped and it gets really hard to reformat it.

I have formatted this one for you. Please check the code is the same as what you have.

avatar image SMASapps · Jan 09, 2013 at 08:35 PM 0
Share

can you answer the question? how do i enable disable the scripts?

avatar image AlucardJay · Jan 12, 2013 at 09:00 AM 0
Share

I answered the question. Can you mark it as correct, or at the least leave a comment?

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by AlucardJay · Jan 08, 2013 at 11:23 AM

What you are doing is enabling/disabling the whole gameObject, not the script. For GameObjects, you should use SetActiveRecursively( true/false ), though this changed for Unity 4 (http://answers.unity3d.com/questions/359394/gameobject-setactiverecursively.html).

Typecast to the names of the scripts :

 var Script1 : NameOfScriptOnObject1;
 var Script2 : NameOfScriptOnObject2;


EDIT :

Create a new scene

Create an empty gameObject, call it HelloObject.

Create a script, call it HelloScript, paste this code :

 #pragma strict
 
 function OnGUI() 
 {
     GUI.color = Color.green;
     GUI.Box( Rect( (Screen.width*0.5) - 120, 40, 100, 35 ), "Hello ...." );
 }

attach HelloScript to HelloObject

Create an empty gameObject, call it GoodbyeObject.

Create a script, call it GoodbyeScript, paste this code :

 #pragma strict
 
 function OnGUI() 
 {
     GUI.color = Color.white;
     GUI.Box( Rect( (Screen.width*0.5), 80, 100, 35 ), ".... Goodbye" );
 }

attach GoodbyeScript to GoodbyeObject

Create an empty gameObject, call it SwapObject.

Create a script, call it SwapScript, paste this code :

 #pragma strict
 
 var theHelloScript : HelloScript;
 var theGoodbyeScript : GoodbyeScript;
 
 function Start() 
 {
     theHelloScript.enabled = false;
     theGoodbyeScript.enabled = false;
 }
 
 function Update() 
 {
     if (Input.GetMouseButtonDown(0))
     {
         theHelloScript.enabled = true;
         theGoodbyeScript.enabled = false;
     }
     else if (Input.GetMouseButtonDown(1))
     {
         theHelloScript.enabled = false;
         theGoodbyeScript.enabled = true;
     }
 }

attach SwapScript to SwapObject

Now, click on SwapObject, and look in the Inspector. There are 2 variables :

 var theHelloScript : HelloScript;
 var theGoodbyeScript : GoodbyeScript;

Now, try and drag HelloObject onto theGoodbyeScript first, see how it won't drop in. Now put it where you think it should really go. Now drag in GoodbyeObject.

Now hit, play, then click the left-mouse-button, then the right-mouse-button.

Comment
Graham-Dunnett
Fattie

People who like this

2 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I disable a wind zone through script? 2 Answers

Turn Off/on culling mask by script? 2 Answers

Unable to enable script 0 Answers

Disable SCRIPT HELP!!!! 1 Answer

Re Enable C# script on camera 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