Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Making a GameObject Appear and Reappear

Discussion in 'Scripting' started by TheGameProgrammer123, Feb 28, 2016.

  1. TheGameProgrammer123

    TheGameProgrammer123

    Joined:
    Feb 22, 2016
    Posts:
    31
    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);

    }
    }

    It just never works...
     
  2. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    SetActive is true by default. You are not setting or resetting it to false anywhere in your script.
     
  3. RavenOfCode

    RavenOfCode

    Joined:
    Apr 5, 2015
    Posts:
    869
    Use
    Code (CSharp):
    1. if (Input.GetKeyDown(KeyCode.A)) gameObject.SetActive(true);
    2. 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.
     
  4. TheGameProgrammer123

    TheGameProgrammer123

    Joined:
    Feb 22, 2016
    Posts:
    31
    Hi there again, meme guy :)
    thanks for your response, I really never knew that :p
     
  5. TheGameProgrammer123

    TheGameProgrammer123

    Joined:
    Feb 22, 2016
    Posts:
    31
    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.
     
  6. RavenOfCode

    RavenOfCode

    Joined:
    Apr 5, 2015
    Posts:
    869
    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?
     
  7. TheGameProgrammer123

    TheGameProgrammer123

    Joined:
    Feb 22, 2016
    Posts:
    31
    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?
     
  8. RavenOfCode

    RavenOfCode

    Joined:
    Apr 5, 2015
    Posts:
    869
    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.
     
  9. TheGameProgrammer123

    TheGameProgrammer123

    Joined:
    Feb 22, 2016
    Posts:
    31
    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
     
  10. RavenOfCode

    RavenOfCode

    Joined:
    Apr 5, 2015
    Posts:
    869
    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.
     
  11. TheGameProgrammer123

    TheGameProgrammer123

    Joined:
    Feb 22, 2016
    Posts:
    31
    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
     
  12. RavenOfCode

    RavenOfCode

    Joined:
    Apr 5, 2015
    Posts:
    869
    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.
     
  13. TheGameProgrammer123

    TheGameProgrammer123

    Joined:
    Feb 22, 2016
    Posts:
    31
    I created A and B. The problem is I have no idea how to reference the objects.
     
  14. RavenOfCode

    RavenOfCode

    Joined:
    Apr 5, 2015
    Posts:
    869
    Just use:
    Code (CSharp):
    1. public GameObject picture;
    2. if (Input.GetKeyDown(KeyCode.A)) picture.SetActive(true);
    3. if (Input.GetKeyUp(KeyCode.A)) picture.SetActive(false);
    4.  
    Now set picture to your picture object in the inspector.
     
  15. TheGameProgrammer123

    TheGameProgrammer123

    Joined:
    Feb 22, 2016
    Posts:
    31
    I am getting around 29 errors.
     
  16. RavenOfCode

    RavenOfCode

    Joined:
    Apr 5, 2015
    Posts:
    869
    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:
    Code (CSharp):
    1. public GameObject picture;
    2. void Update()
    3. {
    4. if (Input.GetKeyDown(KeyCode.A)) picture.SetActive(true);
    5. if (Input.GetKeyUp(KeyCode.A)) picture.SetActive(false);
    6. }
    Then drag the picture into the picture slot in the inspector.
     
  17. TheGameProgrammer123

    TheGameProgrammer123

    Joined:
    Feb 22, 2016
    Posts:
    31
    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
     
  18. RavenOfCode

    RavenOfCode

    Joined:
    Apr 5, 2015
    Posts:
    869
    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).
     
    TheGameProgrammer123 likes this.
  19. TheGameProgrammer123

    TheGameProgrammer123

    Joined:
    Feb 22, 2016
    Posts:
    31
    Everything works just fine. Thank you so much! I really appreciate it! :) Do I need to give credit?
     
    RavenOfCode likes this.
  20. RavenOfCode

    RavenOfCode

    Joined:
    Apr 5, 2015
    Posts:
    869
    Not at all, I'm glad to help. :)

    Best of luck with your project!
     
    TheGameProgrammer123 likes this.
  21. TheGameProgrammer123

    TheGameProgrammer123

    Joined:
    Feb 22, 2016
    Posts:
    31
    Thank you so much. And yes, everyone does start somewhere.

    Also, is there a way where I can make it so that 5 keys can do the same action just for different gameobjects? For example, we covered one, which is A, now I need 4 other ones, which are S, J, K, L, that are all related to different gameobjects.
     
  22. RavenOfCode

    RavenOfCode

    Joined:
    Apr 5, 2015
    Posts:
    869
    Thats totally possible, just make a reference to the other objects and turn them on off with the Input code I provided.
     
    TheGameProgrammer123 likes this.
  23. TheGameProgrammer123

    TheGameProgrammer123

    Joined:
    Feb 22, 2016
    Posts:
    31
    ok thank you :)
     
    RavenOfCode likes this.