Search Unity

EventSystem Not Working (Help Please)

Discussion in 'Scripting' started by Viksy, Oct 22, 2019.

  1. Viksy

    Viksy

    Joined:
    Oct 19, 2019
    Posts:
    4
    Hello,
    I did everything needed to setup my EventSystem (My game is for android) and on PC the buttons and are working perfectly fine, but on phone its a whole new story.The buttons arent aligned as it is on PC, and when I click on the button my hero moves too as I dont have an IsPointerOverGameObject...Please tell me what I am doing wrong, thanks.(This is my Player script and the buttons are from the standard assets) upload_2019-10-23_0-17-31.png
     
  2. Viksy

    Viksy

    Joined:
    Oct 19, 2019
    Posts:
    4
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityStandardAssets.CrossPlatformInput;
    6. using UnityEngine.EventSystems;
    7.  
    8. public class Frog : MonoBehaviour {
    9.  
    10.     public AudioClip MusicClip;
    11.  
    12.     float directionX;
    13.  
    14.     private AudioSource MusicSource;
    15.  
    16.     private Scene scene;
    17.  
    18.     private Rigidbody2D myBody;
    19.  
    20.     void Awake()
    21.     {
    22.         myBody = GetComponent<Rigidbody2D>();
    23.         MusicSource = GetComponent<AudioSource>();
    24.     }
    25.  
    26.     private void Update() {
    27.  
    28.         directionX = CrossPlatformInputManager.GetAxis("Horizontal");
    29.         myBody.velocity = new Vector2(directionX * 8, 0);
    30.  
    31.         if (!EventSystem.current.IsPointerOverGameObject(-1))
    32.         {
    33.  
    34.             if (Input.GetKeyDown(KeyCode.D))
    35.                 myBody.MovePosition(myBody.position + Vector2.right);
    36.  
    37.             if (Input.GetKeyDown(KeyCode.A))
    38.                 myBody.MovePosition(myBody.position + Vector2.left);
    39.  
    40.             if (Input.GetKeyDown(KeyCode.W))
    41.                 myBody.MovePosition(myBody.position + Vector2.up);
    42.  
    43.             if (Input.GetKeyDown(KeyCode.S))
    44.                 myBody.MovePosition(myBody.position + Vector2.down);
    45.  
    46.             if (Input.GetMouseButtonDown(0))
    47.                 myBody.MovePosition(myBody.position + Vector2.up);
    48.         }
    49.     } //update
    50.      
    51.     void Start()
    52.     {
    53.         scene = SceneManager.GetActiveScene();
    54.         MusicSource.clip = MusicClip;
    55.  
    56.     }
    57.  
    58.     void OnTriggerEnter2D(Collider2D target)
    59.     {
    60.  
    61.         if (target.tag == "Car")
    62.         {
    63.             Application.LoadLevel(scene.name);
    64.         }
    65.  
    66.         if (target.tag == "Car")
    67.         {
    68.             MusicSource.Play();
    69.         }
    70.     }
    71.  
    72. } //class
    73.  
    74.  
    It works fine on PC how I want it to but on phone it doesnt
     
    Last edited: Oct 23, 2019