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 Lore_NN · Feb 27, 2014 at 05:25 PM · javascriptenableactivate

Activate and deactivate some script when i press a Key.

Hi to all, I tried to do this script for acrivare and deactivate some script when i press the key B, but how i can assing the specific script? with public var?

 #pragma strict
 
 var script : thescript1; 
 var script : thescript2; 
 var script : thescript3; 
 var script : thescript4; 
 
 
 function Start () {
 
 
  script = GetComponent(thescript1); 
  script = GetComponent(thescript2); 
  script = GetComponent(thescript3); 
  script = GetComponent(thescript4); 
  
  script.enabled = false;
 
 
 }
 
 function Update () {
 
 if (Input.GetKeyDown (KeyCode.B))
 script.enabled = true;
 
 else if (Input.GetKeyDown (KeyCode.B))
 script.enabled = false;
 
 }
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
0

Answer by OrbitSoft · Feb 27, 2014 at 05:50 PM

An efficient way of doing this could be:

 var myScripts1 : MonoBehaviour[]; // Scripts to enable
 var myScripts2 : MonoBehaviour[]; // "     " to disable
 
 void Update()
 {
   if (Input.GetKeyDown (KeyCode.B))
   {
   for (i:int=0;i<myScripts1.Length;i++)
   {
     myScripts1[i].enabled = true;
   }
   } else if (Input.GetKeyUp(KeyCode.B))
   {
   for (i:int=0; i<myScripts2.Length;i++)
   {
     myScripts2[i].enabled = false;
   }
   }
 }

To assign the scripts in the inspector, first attach some script to other gameObjects, then use this script in an empty gameObject, in the inspector set the number of scripts for each array and just drag a gameObject from hierarchy with a script attached.

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 Lore_NN · Feb 27, 2014 at 06:57 PM 0
Share

ther's an error: Unexpected symbol `:' in class, struct, or interface member declaration

avatar image OrbitSoft · Feb 28, 2014 at 02:46 PM 0
Share

Fixed it:

 var myScripts1 : $$anonymous$$onoBehaviour[]; // Scripts to enable
 var myScripts2 : $$anonymous$$onoBehaviour[]; // "     " to disable
  
 function Update()
 {
   if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.B))
   {
   for (var i:int=0;i<myScripts1.Length;i++)
   {
     myScripts1[i].enabled = true;
   }
   } else if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.B))
   {
   for (var b:int=0; b<myScripts2.Length;b++)
   {
     myScripts2[b].enabled = false;
   }
   }
 }
avatar image Lore_NN · Mar 01, 2014 at 09:25 AM 0
Share

it dosn't work :( i created 2 empty gameobect with the active/deactive scripts, and i drag they into inspector (myscript1, and myscript2), but in play mode these two script are enabled. i tried aldo to add:

 function Start ()
 {
 myScripts1.enabled = false;
 myScripts2.enabled = false;
 }

But is an error.

avatar image Lore_NN · Mar 02, 2014 at 10:43 AM 0
Share

someone can help me? o.o

avatar image
0

Answer by RudyTheDev · Feb 27, 2014 at 05:51 PM

In JavaScript vars are public by default. First, you need to name all your variables differently (script1, script2, myScript, someOtherScript...) Then drag&drop your GameObjects with those scripts attached to the script fields in inspector of your main GameObject. Then enable/disable whichever you need.

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

22 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 avatar image avatar image avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

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

Enable a disabled gameobject via javascript? 1 Answer

Activating an Animation? 2 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