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 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
Add comment · 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

· Add your reply
  • Sort: 
avatar image
2
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
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

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

Disable a script from another script 2 Answers

enable/disable specific components 3 Answers

How to Activate Script in another Object? 1 Answer

disable/enable script js 2 Answers

CS0119 Error for Script Disable? 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