Search Unity

OnTouch screen works but UI Button doesnt

Discussion in 'Scripting' started by Kiesco91, May 7, 2019.

  1. Kiesco91

    Kiesco91

    Joined:
    Aug 20, 2018
    Posts:
    80
    hello,

    I have a scene in my gave where a player can just create aloud of fireworks (mini game) but I want to have a button to exit this. I have set up the UI and canvas correctly but it still doesn't work and I think the firework script is overwriting the Button click and is just setting off a firework.

    my Script for fireworks is:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Fireworks : MonoBehaviour
    6. {
    7.  
    8.     List<GameObject> prefabList = new List<GameObject>();
    9.     public GameObject Prefab1;
    10.     public GameObject Prefab2;
    11.     public GameObject Prefab3;
    12.     public GameObject Prefab4;
    13.     public GameObject Prefab5;
    14.     public GameObject Prefab6;
    15.     public GameObject Prefab7;
    16.     public GameObject Prefab8;
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         prefabList.Add(Prefab1);
    22.         prefabList.Add(Prefab2);
    23.         prefabList.Add(Prefab3);
    24.         prefabList.Add(Prefab4);
    25.         prefabList.Add(Prefab5);
    26.         prefabList.Add(Prefab6);
    27.         prefabList.Add(Prefab7);
    28.         prefabList.Add(Prefab8);
    29.  
    30.     }
    31.  
    32.     // Update is called once per frame
    33.     void Update()
    34.     {
    35.         if (Input.touchCount > 0)
    36.         {
    37.             Touch touch = Input.GetTouch(0);
    38.             Vector2 touchPos = Camera.main.ScreenToWorldPoint(touch.position);
    39.  
    40.             if (touch.phase == TouchPhase.Began)
    41.          
    42.             {
    43.             int prefabIndex = UnityEngine.Random.Range(0, 8);
    44.             Instantiate(prefabList[prefabIndex], touchPos, Quaternion.identity);
    45.  
    46.             }
    47.  
    48.         }
    49.     }
    50. }
    it there something I need to do to make a button work?

    thanks,
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
  3. Kiesco91

    Kiesco91

    Joined:
    Aug 20, 2018
    Posts:
    80
    Ooo I did look at this but I thought it was only for mouse click and not finger touch but I've noticed the section at the bottom.

    Will try this tonight. Thanks!