Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Oct 10, 2016 at 04:59 AM by nj4242 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by nj4242 · Oct 07, 2016 at 06:31 AM · gameobjectapplicationactivekeypressdeactivate

activate object on key press only, otherwise always deactivated!

Hi,

I need to create a simple code for an object to activate on a 'key' press only, otherwise always it should be deactivated. The problem I have is that it doesn't functions as I wanted, instead it toggles, pressing key results into activation on and another press make the object deactivate. All I need is just one press key activate object and deactivate when I lift the key up. Also tried GetKeyDown() and GetKeyUp() but it ends up toggling it. Here's my non-working code:

 void Update () {
 
         animObject.SetActive(false);
 
         if (Input.GetKeyDown(KeyCode.Space))
         {
             animObject.SetActive(true);
             Application.CaptureScreenshot("screeshot.png");
         }
     }    

My purpose : Is to take a screenshot of the object while key is pressed and take a screenshot of it, and then deactivate object such that it doesn't seen on a visible scale, The object must be invisible (deactivated) all the time. Thanks

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 RomanLed · Oct 07, 2016 at 11:35 PM 1
Share

create a bool and set it when you click the key the first time, use the bool to prevent the code from executing again, then reset it when you use Get$$anonymous$$eyUp

2 Replies

  • Sort: 
avatar image
2
Best Answer

Answer by RomanLed · Oct 07, 2016 at 11:55 PM

i know you said you tried this however is this the code you used? it could have been toggling because in the script you posted you are setting animObject.SetActive(false) every frame.

     void Update()
     {
         if (Input.GetKeyDown(KeyCode.Space))
         {
 animObject.SetActive(true);
 Application.CaptureScreenshot("screeshot.png");
         }
 
         if (Input.GetKeyUp(KeyCode.Space))
         {
 animObject.SetActive(false);
         }
     }
Comment
Add comment · Show 2 · 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 nj4242 · Oct 10, 2016 at 04:56 AM 0
Share

Thanks @RomanLed. It did worked in the end. :))

avatar image RomanLed · Oct 10, 2016 at 11:50 AM 1
Share

cool glad it worked :)

avatar image
1

Answer by saschandroid · Oct 07, 2016 at 07:35 AM

Like this?

     private void Update () 
     {
          if (Input.GetKeyDown(KeyCode.Space))
         {
             animObject.SetActive(true);
             StartCoroutine(Capture());
         }
     }

     IEnumerator Capture()
     {
         Application.CaptureScreenshot("screenshot.png");
         yield return new WaitForEndOfFrame();        
         animObject.SetActive(false);
     }

Edit: I updated my answer according to your comment.

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 nj4242 · Oct 07, 2016 at 10:55 AM 0
Share

Hey @saschandroid, Thanks for your reply, your code is completely true, and as I mentioned in the question that I tried it. But it's not fitting in my context or not working precisely. I wanted to take a screenshot in a split second when the object becomes visible, after that I need to make sure it never shows up anyway. So, The Obvious problem is that when I wanted the game Object to be deactivated all the time, since the object is deactivated it'll surely won't be identified as the object is not activated. And I don't have script on animObject, it's attached with camera.

avatar image b1gry4n nj4242 · Oct 07, 2016 at 11:37 PM 1
Share

If you have a reference to the game object, whether the state is active or not, your reference to that object will not be broken. You can freely turn it on or off. If youre turning the game object off that the script is attached to, the script will be disabled. The solution is to move the script off the game object.

The other solution, is to enable a camera that can see the object and your main camera can not see the object. Render the "screenshot camera" behind the main cameras depth. You could leave the object on at all times, if you wanted, using this method as your main camera wouldnt be drawing it. It just needs to be set on the right layer and the culling masks of your cameras set up correctly

avatar image nj4242 · Oct 10, 2016 at 04:58 AM 0
Share

Thanks, the suggestion you gave before edit did worked actually. Since, the previous code worked for me, I haven't tried the new one. :))

avatar image saschandroid nj4242 · Oct 10, 2016 at 07:00 AM 1
Share

It is actually more or less the same as the later answer you accepted. The difference is that I put taking the screenshot into a coroutine to make sure animObject is shown at least for one frame (I'm not sure if this is needed but I remember that I had a problem with capturing a screenshot where the gui wasn't deactivated long enough in a similar situation).

avatar image nj4242 saschandroid · Oct 10, 2016 at 12:36 PM 0
Share

I'm considering it, I even said so. I managed to to worked out the code you gave before the edit. Though thanks for letting me know. Thanks Again. :))

Follow this Question

Answers Answers and Comments

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

Related Questions

Children of DualTouchControls prefab keep being forced to active 0 Answers

activating and deactivating gameObject on keypress? 1 Answer

Referencing children of an instantiated object. 1 Answer

How do I change a gameobject to be active through a script? 1 Answer

Deactivate an object - and all scripts in that object deactivated? 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