Making a GameObject Appear and Reappear

I am trying to make an GameObject appear if you press a button and when you release it dissapears…
I am having a very hard time doing this…
Please help me! :slight_smile:

using UnityEngine;
using System.Collections;

public class Arrow : MonoBehaviour {

// Use this for initialization
void Start()
{

}

// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(“a”)) ;
this.gameObject.SetActive(true);

}
}

It just never works…

SetActive is true by default. You are not setting or resetting it to false anywhere in your script.

Use

if (Input.GetKeyDown(KeyCode.A)) gameObject.SetActive(true);
if (Input.GetKeyUp(KeyCode.A)) gameObject.SetActive(false);

As far as your code goes it seems you are trying to access the Input class but the syntax you are using it wrong, it will never return true and probably will give you errors in the console.

Hi there again, meme guy :slight_smile:
thanks for your response, I really never knew that :stuck_out_tongue:

Thank you so much. It is still not working though…

Do I have to disable the game object or something? Whenever I run the game the game object is still visible. I need to be invisible and when you press a it will appear, and when you release a it will turn invisible.

Is the script active? Are you setting the correct object to true? Is the code in Update? Is the script on the gameobject that you are turning off?

The script is active. The code is in update. The code is attached to the gameobject. I do not know what you mean by setting the correct object to true. Do you mean the part of the code?

Try using a different object to set the object true/false, the problem is that once you set the object to false it cant be turned back on from the script if the script is attached to the object set false.

Okay.

It is an empty game object, with a picture on it and a transparent shader.
Right now, it is enabled, with a script attached to it, along with a halo.

Is everything OK or not?

Let me try a new one

Create two objects: A and B, lets have A be the one you want to turn on/off, use B to have the script on and reference A in the script attached to B.

Wait. I added a cube to the scene and attached the script to the cube. Now, when I press A, both of the game objects turn off whenever I press A and let go, and when I press A it doesnt appear again

Did you unattach the script from the Picture object? Then what you need to do is reference the Picture object in the script on the cube. Also using an empty would have worked just fine.

I created A and B. The problem is I have no idea how to reference the objects.

Just use:

public GameObject picture;
if (Input.GetKeyDown(KeyCode.A)) picture.SetActive(true);
if (Input.GetKeyUp(KeyCode.A)) picture.SetActive(false);

Now set picture to your picture object in the inspector.

I am getting around 29 errors.

It seems you dont really know the basics of unity, maybe look at some tutorials? As I would guess your errors are because the code I gave you was either all put in update or not put in update. Maybe you also forgot to assign picture in the inspector. Just do this:

public GameObject picture;
void Update()
{
if (Input.GetKeyDown(KeyCode.A)) picture.SetActive(true);
if (Input.GetKeyUp(KeyCode.A)) picture.SetActive(false);
}

Then drag the picture into the picture slot in the inspector.

Omg, damn it. I am so sorry. I am really just a beginner…Okay. I did everything and it works, except for when I press A again it turns on and turns back invisible. By the way, I need it so thats why first its invisible and then after when you press it it will turn visible, and then release and it will turn off. And it can go back and forth back and forth

Thats fine everyone starts somewhere. :slight_smile: If you want to start it invisible just turn it off in the inspector (in the little checkbox next to the name of the object).

1 Like

Everything works just fine. Thank you so much! I really appreciate it! :slight_smile: Do I need to give credit?

1 Like

Not at all, I’m glad to help. :slight_smile:

Best of luck with your project!

1 Like