- Home /
The question is answered, right answer was accepted
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
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
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);
}
}
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.
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.
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
Thanks, the suggestion you gave before edit did worked actually. Since, the previous code worked for me, I haven't tried the new one. :))
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).
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
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