Activate Object with Script

Hi, im wanna activate a gameobject with an keypress.

My script is:

function Update()
{
if(Input.GetKeyDown(KeyCode.Escape)){
gameObject.SetActive(false);
}
}

i know that i unity doesnt recognize deactivatet objects, but i cant find a solution for it.

Please use code tags found on the insert button next time you post up your code, it makes life much easier for people having to look at stuff.

I don’t know if this was a mistake in copy and pasting your code, but your first issue is that function Update should be Void Update, I highly recommend looking at basic tutorials so you can work from the ground up on things and know what you’re doing.

gameObject.SetActive should also be set to True, if you want to get really fancy you can use an else statement and have the SetActive to false and so on if you press the key again. If you’re trying to make a shortcut key for a UI example this will deactivate the Window if you let go or depending on how you code it you can have the same button close it after you press again.

I can’t stress enough that if you’re new you should go through all of the official Unity tutorials especially for the basic code, it will save you a lot of headaches in the future.

Edit: Oh wait, my bad, I just realised that you were using Javascript as well, but the same rules should apply because this is quite basic code thankfully. TLDR is that whenever you’re pressing the escape key you are actually deactivating your object which will explain why nothing is happening especially if you’ve already got it deactivated.

True and False turn on and off the function respectively, True = On, False = Off, that’s how I remember it.

1 Like

[QUOTE="
True and False turn on and off the function respectively, True = On, False = Off, that’s how I remember it.[/QUOTE]

I know, thats a mistake with copying. This code is working, my gameobject becoms deactivatet. But with the same script with true insteat of false i cant activate the object.

It isn’t that unity doesn’t recognise inactive objects, it knows they are there. However, it doesn’t call the update function on them.

What you would need to do is activate the gameobject from a script which attached to an active gameobject.

1 Like

I was just wondering actually looking at the code if that’s exactly what he’s done, I should have asked first, it is attached right? gameObject only gets the GameObject that has the script attached to it.

Okay, now that I’ve had some coffee I’m reading and thinking better now, if you’ve got the object activated you need to deactivate by clicking the tick on the top right in the editor as shown it the tutorial when you have the object selected, then the object should activate properly when you press play and press the key.

I cant find in the tutroial what i am looking for [maybe its caused by my bad english] i i i want to make a pause screen. by starting the game is the ui activatet and with pressins esc i can close the ui. but if im deacitvating the ui and put the my script (.setActive(true)) in it i cant activate it. i tried to use .activeSelf but im not sure if i unterstand what the function makes and im always getting an error.

Post up the error so we can take a look at it, have you made sure that the Pause screen is parented to a canvas? If it isn’t then it won’t even show up when you press play.

I already deletet the script and yes its in a canvas. If the ui is activatet and im starting the game i can deactivate it with .setActive(false) but when its deactivatet (it doesnt matter if i deactivatet ingame or in unity) a cant activate it. i never get a error message with .setActive(true) it just wont work.