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 Tyler 2 · Jan 11, 2011 at 12:33 AM · ifactivatefalsetrue

Object activation...

Hello. This is my code for a star fox style shield (the button is pressed, the shield object is active for a second, and then it is not active). If I press q that happens, the shield become active and then after a second, it deactivates. The problem is that I cannot do it again, when I press q nothing happens. What is wrong? Thanks

function Update(){ if( Input.GetKeyDown( "q")){Invoke("ShieldON",0);} else {Invoke("ShieldOFF",1);} }

function ShieldON (){ SHIELD.active=true; } function ShieldOFF(){ SHIELD.active=false; }

Comment
Add comment · Show 2
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 Bunny83 · Jan 11, 2011 at 03:41 AM 0
Share

What behaviour do you want? when you press and hold the button down the shield should be on and when you release the button after 1 sec it goes off? or just when you press "q" it turns on for a sec?

avatar image Tyler 2 · Jan 11, 2011 at 04:47 AM 0
Share

When you press q it turns on for a second (if that works, I could probably figure out some sort of cooldown system).

2 Replies

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

Answer by Bunny83 · Jan 11, 2011 at 03:49 AM

Your problem is that you invoke ShieldOFF every frame (every invoke get delayed 1 sec but after that sec ShieldOFF is called ... every frame)

Input.GetKeyDown() is only true for one frame (because it just tells you the event "Keydown") that means in the next frame ShieldOFF is called again.

The second script that Justin Warner posted above should work.

You can change your script to:

function Update(){ if( Input.GetKeyDown( "q")){ ShieldON(); Invoke("ShieldOFF",1); } }

function ShieldON (){ SHIELD.active=true; } function ShieldOFF(){ SHIELD.active=false; }

ShieldON don't need to be invoked that way when you don't want a delay. Just call the function. Now when you press "q" ShieldON is called instantly and turn your shield on. But also Invoke ShieldOFF with 1 sec. delay. After that sec. ShieldOFF should be called.

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

Answer by Justin Warner · Jan 11, 2011 at 12:57 AM

function Update(){ if(Input.GetKeyDown("q")) { Invoke("ShieldON",0); Debug.Log("Shield on"); } else { Invoke("ShieldOFF",1); } }

function ShieldON (){ SHIELD.active=true; } function ShieldOFF(){ SHIELD.active=false; }

Try that, and tell me when/if it keeps saying Shield On in the log...

Also, this might work:

function Update(){ if(Input.GetKeyDown("q")) { Invoke("ShieldON",0); Debug.Log("Shield on"); } }

function ShieldON (){ SHIELD.active=true; yield WaitForSeconds(2); SHIELD.active=false; }

Reply how it goes please.


var SHIELD : GameObject; var activate = 0; function Update(){ if(Input.GetKeyDown("q") && activate == 0) { SHIELD.active=true; activate = 1; } if(Input.GetKeyDown("q") && activate == 1) { SHIELD.active=false; activate = 0; }

}

You might be able to figure it out from there. Sorry I couln't help further. I had this problem as well, and I got a solve, and I believe it was like this, but it still doesn't work =/. Good luck none-the-less!

Comment
Add comment · Show 6 · 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 Tyler 2 · Jan 11, 2011 at 01:30 AM 0
Share

"Shield On" appears in the log with both scripts...but it will only be added to the log once. No matter how many times I press q. The SHIELD object will also not turn on.

avatar image Justin Warner · Jan 11, 2011 at 01:44 AM 0
Share

Does it say Shield On whenever you push q a lot though? Like, is it registering that it is being pushed multiple times?

And how are you getting SHIELD?

avatar image Tyler 2 · Jan 11, 2011 at 02:32 AM 0
Share

Yeah, it says Shield On for each button press. SHIELD (the object I want to turn on/off) is just a variable "var SHIELD : GameObject;"

avatar image Justin Warner · Jan 11, 2011 at 03:18 AM 0
Share

I've been testing it, I know the problem, but I can't get it to work right on my own... $$anonymous$$aybe you can figure it out... The problem is, it is running every frame, so when you push down q, it enables it for a frame, which as you probably know, can be a hundreth of a second, easily... So, rather than that, you need to find a way to get it to just stop... So, the else statement can't be there... I tried a mode kinda thing, I'll edit it with that, but it didn't work... But again, maybe you'll be able to figure it out.

avatar image Bunny83 · Jan 11, 2011 at 03:37 AM 1
Share

Your second script should do what he needs the third script is just a toggle script: press the button once = on press it again = Off

that would be easier that way: if(Input.Get$$anonymous$$eyDown("q")) { SHIELD.active = !SHIELD.active; }

Show more comments

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

activating object only on true 1 Answer

A bunch of errors that I can't fix 1 Answer

How to use if statement properly 1 Answer

My if statement won't work 1 Answer

Boolean while looking at a game object? 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