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 Chris 43 · Apr 23, 2011 at 10:40 PM · buttontoggleswitch

Toggle Scripts On & Off

Lets say i press Q and it does

GetComponent(Script).enabled = false;

Ofcourse that works great one way but how can i set it so when i press Q again it will do

GetComponent(Script).enabled = true;

Like a switch between them. Javascript btw...

Thanks :) Also im only learning its most likely a very simple solution but no idea how :(

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Justin Warner · Apr 23, 2011 at 11:22 PM

http://unity3d.com/support/documentation/ScriptReference/Input.html

Read up on that.

But:

function Update () {
  if (Input.GetButtonDown ("q")) {
    if(GetCompnent(Script).enabled=true)
    {
      GetCompnent(Script).enabled = false;
    }
    else
    {
      GetCompnent(Script).enabled = true;
    }
  }
}
Comment
Add comment · Show 5 · 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 Joshua · Apr 23, 2011 at 11:55 PM 1
Share

Right, just make sure the script you reference is not the script youre executing this from. Because then when you turn it of, it can no longer register if you turn it on :p

avatar image Justin Warner · Apr 24, 2011 at 12:53 AM 0
Share

Lol. Yep XD..........

avatar image Kobohdashi · Aug 11, 2013 at 07:31 PM 0
Share

So I can put this in an empty game object then have an on screen button to switch between the characters(scripts)?

avatar image Joyrider · Aug 11, 2013 at 07:38 PM 0
Share

indeed, with some adjustments, you could put this on empty game object, reference your character objects/scripts. And have a button on screen to switch between them.

But the basic logic is here.

avatar image yami7 · May 16, 2014 at 09:16 PM 0
Share

but in c# it says that GetComponent("Script name" ) doesn't have method ".enabled" why so??

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Switch between mouse controller character to controlling camera? 0 Answers

Unity Script help for on/off switch code. 1 Answer

How To destroy specific objects? 2 Answers

GUI Button which looks like a Toggle 0 Answers

How to reset the state of other buttons when clicking on one of them 0 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