Search Unity

Problems when building from Unity

Discussion in 'Editor & General Support' started by CrazyCGChick85, Nov 3, 2019.

  1. CrazyCGChick85

    CrazyCGChick85

    Joined:
    Aug 30, 2018
    Posts:
    19
    Hi All,

    I have been doing the Ultimate Guide to Game Development in Unity on Udemy and all was going well until I started the sessions on build.

    When I play the game within Unity I have no issues everything works fine. However, when I build to PC, MAC and Linux the player (without any input from me) starts to fall off-screen to the left. I also have no ability to move along Y-axis. If I hold down the right arrow the player does start to move to the right.

    I have requested help on the course Q and A and the facebook page but it's either been silence or any suggestion doesn't work. I am wondering if this is a bug?



    Player code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     private float _speed = 3.5f;
    9.     [SerializeField]
    10.     private float _fireRate = 0.5f;
    11.    
    12.     [SerializeField]
    13.     private GameObject _laserPrefab;
    14.     [SerializeField]
    15.     private GameObject _tripleShotPrefab;
    16.     [SerializeField]
    17.     private GameObject _shield;
    18.     [SerializeField]
    19.     private GameObject _rtEngine;
    20.     [SerializeField]
    21.     private GameObject _ltEngine;
    22.  
    23.     [SerializeField]
    24.     private int _lives = 3;
    25.     [SerializeField]
    26.     private int _score;
    27.  
    28.     [SerializeField]
    29.     private AudioSource _laserAudio;
    30.  
    31.     private float _speedMultiplier = 2;
    32.     private float _canFire = -1f;
    33.  
    34.     private bool _isTripleShotActive = false;
    35.     private bool _isSpeedBoostActive = false;
    36.     private bool _isShieldBoostActive = false;
    37.  
    38.  
    39.     private UIManager _uimanager;
    40.     private SpawnManager _spawnManager;
    41.  
    42.  
    43.  
    44.     // Start is called before the first frame update
    45.     void Start()
    46.     {
    47.         transform.position = new Vector3(0, 0, 0);
    48.  
    49.         _spawnManager = GameObject.Find("Spawn_Manager").GetComponent<SpawnManager>();
    50.         _uimanager = GameObject.Find("Canvas").GetComponent<UIManager>();
    51.  
    52.         if (_spawnManager == null)
    53.         {
    54.             Debug.LogError("The Spawn Manager is NULL.");
    55.         }
    56.  
    57.         if(_uimanager == null)
    58.         {
    59.             Debug.LogError("The UI Manager is Null");
    60.         }
    61.  
    62.     }
    63.  
    64.     // Update is called once per frame
    65.     void Update()
    66.     {
    67.         CalculateMovement();
    68.  
    69.         if (Input.GetKeyDown(KeyCode.Space) && Time.time > _canFire)
    70.         {
    71.             FireLaser();
    72.         }
    73.     }
    74.  
    75.     void CalculateMovement()
    76.     {
    77.         float horizontalInput = Input.GetAxis("Horizontal");
    78.         float verticalInput = Input.GetAxis("Vertical");
    79.  
    80.         //new Vector3(1, 0, 0) * variable * 3.5f * real time can be converted to below
    81.        //transform.Translate(Vector3.right * horizontalInput * _speed * Time.deltaTime);
    82.         //transform.Translate(Vector3.up * verticalInput * _speed * Time.deltaTime);
    83.  
    84.         Vector3 direction = new Vector3(horizontalInput, verticalInput, 0);
    85.  
    86.         transform.Translate(direction * _speed * Time.deltaTime);
    87.  
    88.         transform.position = new Vector3(transform.position.x, Mathf.Clamp(transform.position.y, -3.8f, 0));
    89.  
    90.  
    91.         if (transform.position.x >= 11.3f)
    92.         {
    93.             transform.position = new Vector3(-11.3f, transform.position.y, 0);
    94.         }
    95.         else if (transform.position.x <= -11.3f)
    96.         {
    97.             transform.position = new Vector3(11.3f, transform.position.y, 0);
    98.         }
    99.     }
    100.  
    101.     void FireLaser()
    102.     {
    103.         _canFire = Time.time + _fireRate;
    104.  
    105.         if (_isTripleShotActive == true)
    106.         {
    107.             Instantiate(_tripleShotPrefab, transform.position, Quaternion.identity);
    108.         }
    109.         else
    110.         {
    111.             Instantiate(_laserPrefab, transform.position + new Vector3(0, 1.05f, 0), Quaternion.identity);
    112.         }
    113.  
    114.         _laserAudio.Play();
    115.  
    116.  
    117.     }
    118.  
    119.     public void Damage()
    120.     {
    121.         if (_isShieldBoostActive == true)
    122.         {
    123.             _isShieldBoostActive = false;
    124.             _shield.SetActive(false);
    125.  
    126.             return;
    127.         }
    128.  
    129.         _lives -= 1;
    130.  
    131.         _uimanager.UpdateLives(_lives);
    132.  
    133.         if (_lives == 2)
    134.         {
    135.             _ltEngine.SetActive(true);
    136.         }
    137.  
    138.         else if (_lives == 1)
    139.         {
    140.             _rtEngine.SetActive(true);
    141.         }
    142.  
    143.         if (_lives < 1)
    144.         {
    145.             _spawnManager.onPlayerDeath();
    146.             Destroy(this.gameObject);
    147.             _uimanager.GameOver();
    148.  
    149.  
    150.         }
    151.     }
    152.  
    153.     public void TripleShotActive()
    154.     {
    155.         _isTripleShotActive = true;
    156.  
    157.  
    158.         StartCoroutine(TripleShotPowerDown());
    159.  
    160.     }
    161.  
    162.     IEnumerator TripleShotPowerDown()
    163.     {
    164.             yield return new WaitForSeconds(5.0f);
    165.             _isTripleShotActive = false;
    166.  
    167.     }
    168.  
    169.     public void SpeedBoostActive()
    170.     {
    171.         _isSpeedBoostActive = true;
    172.         _speed *= _speedMultiplier;
    173.         StartCoroutine(SpeedBoostPowerDown());
    174.     }
    175.  
    176.     IEnumerator SpeedBoostPowerDown()
    177.     {
    178.  
    179.         yield return new WaitForSeconds(5.0f);
    180.         _isSpeedBoostActive = false;
    181.         _speed /= _speedMultiplier;
    182.     }
    183.  
    184.     public void ShieldBoostActive()
    185.     {
    186.         _isShieldBoostActive = true;
    187.         _shield.SetActive(true);
    188.     }
    189.  
    190.     public void AddScore(int points)
    191.     {
    192.         _score += points;
    193.         _uimanager.UpdateScore(_score);
    194.     }
    195.  
    196.  
    197. }
    198.  
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    Build issues can be a little hard to track down, as it's somewhat more complex to gather diagnostics in a build. But you can still do it.

    In this case, I'd probably start by adding some simple logging to your Update statement, that prints the raw Horizontal and Vertical axis inputs it's registering. If you print those values with Debug.LogError (not Debug.Log, but LogError) they should get shown on the screen in an ugly window in your build, but it means you won't have to go track down the log file. Anyway, I would assume you're getting bad inputs, but this should verify it.

    Next, on the small dialog that pops up to allow screen resolution changes, go to the Input tab. See if you have any weird entries for horizontal and vertical. It's possible you'll need to increase the "dead" setting for the horizontal axis.

    Anyway, I'm not yet sure what this would be happening, but you'll probably just need to gather more info to track it down.
     
    CrazyCGChick85 likes this.
  3. CrazyCGChick85

    CrazyCGChick85

    Joined:
    Aug 30, 2018
    Posts:
    19
    Thanks

    I did a WebGL build and had no issues with that it all worked perfectly which again is odd

    Will try these suggestions