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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Beginner question: how to make a sphere popup when my finger touches a cube?

Discussion in 'Scripting' started by lehula, Jul 22, 2015.

  1. lehula

    lehula

    Joined:
    May 2, 2013
    Posts:
    5
    I want to make a sphere popup, just above a cube, when I touch the cube. The sphere will initially be inactive. I just want to activate it when the cube is touched. This is what I have, but it doesn't work. Any guidance would be greatly appreciated.

    void Update () {
    if (Input.touchCount > 0) {
    if (Input.GetTouch (0).phase == TouchPhase.Ended) {

    gameObject.SetActive (true);
    }
    }
    }
     
  2. aoe_labs

    aoe_labs

    Joined:
    Nov 4, 2013
    Posts:
    42
    The first half of what you wrote, the update function, is aimed at the cube. The second half is aimed at the sphere. So this script cannot be attached to the sphere. The first half is checking for input, and therefore the object it is attached to, the cube, must be active. If you were to put the second half on the sphere, it wouldn't work, because a script on an inactive game object won't work.

    You can do something like this:
    Code (CSharp):
    1. public GameObject sphere;
    2.  
    3. void Update()
    4. {
    5. if (Input.touchCount > 0)
    6. {
    7. if (Input.GetTouch(0).phase = TouchPhase.Ended)
    8.   {
    9.     sphere.SetActive (true);
    10.     //sphere.transform.position = new Vector3(transform.position.x, transform.position.y + 5, transform.position.z);
    11.    }
    12. }
    13.  
    14.  
    15. }
    The first line makes a reference to the sphere, so the script knows what object it needs to set active. You would attach this to the cube, and drag in the sphere in the inspector.

    The commented out line will make the sphere appear 5 units above the cube in the y direction. So, wherever the cube may be, the sphere will always be 5 units above it when activated. You can adjust the values accordingly in that line accordingly to have it positioned wherever you need. i.e. if you only want it 2 units above, change the 5 to a 2.