Search Unity

Need help developing my game

Discussion in 'Android' started by johnys113, Mar 21, 2019.

  1. johnys113

    johnys113

    Joined:
    Mar 21, 2019
    Posts:
    3
    Hey. So I am trying to code a game for Android, and i have a lot of troubles, because i had big expectations, but i just don't know how to do it. I am beginner and I would love if you could help me.

    So. I want to code a game, that shows a button(or gameobject that looks like button), and when you touch it, it disappears and appear randomly on the screen - till user ends the game (I then want to do it with time, so if you wont press it until -idk- 2 secs, it's game over, but i think that is not that hard to code ). I know it sounds easily, but i have tried to run this game many times, but i dont know, how to do it.

    Could you please help me ?
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,445
    which part your are having problem with?

    Does clicking the object already work?
     
  3. johnys113

    johnys113

    Joined:
    Mar 21, 2019
    Posts:
    3
    I am having problem with literally everything. :D firstly i tried this game with UI button, but all I could do is just destroy that button after clicking.
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,445
    you can disable the button and its image components, or the whole button gameobject to completely hide it.

    do that from separate script, since if you disable the gameobject, scripts wont run on that gameobject anymore.
     
  5. johnys113

    johnys113

    Joined:
    Mar 21, 2019
    Posts:
    3
    But I really dont know how, could you please include some code?
     
  6. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,445
    try something like,

    attach this script to empty gameobject,
    then assign your button into that field,
    then in the button OnClick even, assign your empty gameobject with the script,
    and select the Hide method from that dropdown:
    upload_2019-3-21_15-11-28.png

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class HideButton : MonoBehaviour
    4. {
    5.     // assign your button reference here
    6.     public GameObject mybuttonGameObject;
    7.  
    8.     // this method is called from button click
    9.     public void Hide()
    10.     {
    11.         // disable gameobject to hide it
    12.         mybuttonGameObject.SetActive(false);
    13.  
    14.         // after 2seconds, show the button again
    15.         Invoke("ShowButton", 2);
    16.     }
    17.  
    18.  
    19.     void ShowButton()
    20.     {
    21.         // TODO you could then randomize the position here
    22.  
    23.         mybuttonGameObject.SetActive(true);
    24.     }
    25.  
    26. }
    27.