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!
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);
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.
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.
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.
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.
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. 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).