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 h4wkeye · Mar 29, 2011 at 01:52 PM · fpsgame

Disabling a GameObject via keyboard

Hello fellow game devs! :)

I am trying to get a script working which disables a game Object if i press a certain button on the keyboard.

f.e

if(Input.GetKey("1"))
{
 spotlight1.enabled = !spotlight1.enabled;
}

it is not working :/

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 Justin Warner · Mar 29, 2011 at 01:59 PM 0
Share

Please use the 10101 button up top to code-itize your code =).

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Mike 3 · Mar 29, 2011 at 01:57 PM

Your need to use GetKeyDown or GetKeyUp, otherwise it'll call your toggle every frame

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 h4wkeye · Mar 29, 2011 at 01:58 PM 0
Share

Thank you for the fast and kind response

avatar image h4wkeye · Mar 29, 2011 at 02:00 PM 0
Share

But now the compiler is complaining about my variables...can i actually manipulate my game objects by calling them with the names i gave em? (spot1, spot2, etc)

avatar image Mike 3 · Mar 29, 2011 at 02:03 PM 0
Share

You could use something like var spot1 = GameObject.Find("spotlight1").light; spot1.enabled = !spot1.enabled;

avatar image h4wkeye · Mar 29, 2011 at 02:05 PM 0
Share

i used directional lights and named em spot1 - spot5 what i want to do is

if(Input.Get$$anonymous$$eyDown("1")) { spot1.enabled = !spot1.enabled; }

avatar image
1

Answer by h4wkeye · Mar 29, 2011 at 02:19 PM

Nevermind, guys.

here's the solution. :O

var target1 = GameObject.Find("spot1").light; var target2 = GameObject.Find("spot2").light; var target3 = GameObject.Find("spot3").light; var target4 = GameObject.Find("spot4").light; var target5 = GameObject.Find("spot5").light;

function Update() { if(Input.GetKeyDown("1")) { target1.enabled = !target1.enabled; } if(Input.GetKeyDown("2")) { target2.enabled = !target2.enabled; } if(Input.GetKeyDown("3")) { target3.enabled = !target3.enabled; } if(Input.GetKeyDown("4")) { target4.enabled = !target4.enabled; } if(Input.GetKeyDown("5")) { target5.enabled = !target5.enabled; } }

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

No one has followed this question yet.

Related Questions

Horror Game AI script recommendation? 1 Answer

FPSMouseScript.js 1 Answer

Bullet Fire script not working 1 Answer

[Closed] MainMenu Script error 2 Answers

a strange problem ? 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