Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Script refuses to work in APK but works in Unity Remote/Editor.

Discussion in 'Scripting' started by tomowale, Apr 21, 2017.

  1. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    (Repost to correct thread)
    Hey guys , I have a script attached to a car game object that allows the player to tap the screen and then accelerate , it works fine in Unity Remote , but when I download the APK , as soon as I tap the screen , collisions go completely bezerk and the car just starts ignoring collisions , what's going on? As I need this to work to complete my game , as the player can move side to side with the accelerometer but I also want them to move forward when they touch the screen. Also , my game is 2D.
    The code I think is causing the error starts at line 32
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class carController : MonoBehaviour {
    4.     public float carSpeed;
    5.     public float maxPos = 2.2f;
    6.     Vector3 position;
    7.     public UIManager ui;
    8.     public AudioManager am;
    9.     public float MaxPosY = 3.69f;
    10.     public GameObject anim;
    11.     bool currntPlatformAndroid = false;
    12.     bool heldDown = true;
    13.     Rigidbody2D rb;
    14.     void Awake(){
    15.         rb = GetComponent<Rigidbody2D> ();
    16.         #if UNITY_ANDROID
    17.                 currntPlatformAndroid = true;
    18.         #else
    19.                 currentPlatformAndroid = false;
    20.         #endif
    21.         am.carSound.Play ();
    22.     }
    23.     void Start () {
    24.         position = transform.position;
    25.         if (currntPlatformAndroid == true) {
    26.             Debug.Log ("Android");
    27.         }
    28.         else {
    29.             Debug.Log ("Windows");
    30.         }
    31.     }
    32.     void FixedUpdate() {
    33.         if (Input.touchCount > 0)
    34.         {
    35.             transform.Translate(new Vector3(0, 1, 0) * carSpeed * Time.deltaTime);
    36.         }
    37.         else {
    38.             transform.Translate(new Vector3(0, -1, 0) * carSpeed * Time.deltaTime);
    39.         }
    40.     }
    41.     void Update()
    42.     {
    43.         if (currntPlatformAndroid == true)
    44.         {
    45.             //android specific code
    46.             //TouchMove ();
    47.             AccelerometerMove();
    48.         }
    49.         else {
    50.             position.x += Input.GetAxis("Horizontal") * carSpeed * Time.deltaTime;
    51.             position.y = Mathf.Clamp(position.y, -4.25f, 4.25f);
    52.             position.x = Mathf.Clamp(position.x, -2.5f, 2.5f);
    53.             transform.position = position;
    54.         }
    55.  
    56.         position = transform.position;
    57.         position.y = Mathf.Clamp(position.y, -4.25f, 4.25f);
    58.         position.x = Mathf.Clamp(position.x, -2.5f, 2.5f);
    59.         transform.position = position;
    60.  
    61.     }
    62.  
    63.     void OnCollisionEnter2D(Collision2D col){
    64.         if (col.gameObject.tag == "EnemyCar") {
    65.             //Destroy (gameObject);
    66.             Instantiate(anim, transform.position, transform.rotation);
    67.             am.explosionSound.Play();
    68.             Destroy(anim);
    69.             Destroy(this.gameObject);
    70.             am.carSound.Stop();
    71.             ui.gameOverorNot();
    72.         }
    73.     }
    74.     void AccelerometerMove(){
    75.         float x = Input.acceleration.x;
    76.         Debug.Log ("X = " + x);
    77.         if (x < -0.1f) {
    78.             MoveLeft ();
    79.         } else if (x > 0.1f) {
    80.             MoveRight ();
    81.         }
    82.         else {
    83.             SetVelocityZero();
    84.         }
    85.     }
    86.     void TouchMove(){
    87.         if (Input.touchCount > 0) {
    88.             Touch touch = Input.GetTouch (0);
    89.             float middle = Screen.width / 2;
    90.             if (touch.position.x < middle && touch.phase == TouchPhase.Began) {
    91.                 MoveLeft ();
    92.             }
    93.             else if (touch.position.x > middle && touch.phase == TouchPhase.Began) {
    94.                 MoveRight ();
    95.             }
    96.  
    97.         }
    98.         else {
    99.             SetVelocityZero();
    100.         }
    101.     }
    102.     public void MoveLeft(){
    103.         rb.velocity = new Vector2 (-carSpeed, 0);
    104.     }
    105.     public void MoveRight(){
    106.  
    107.         rb.velocity = new Vector2 (carSpeed, 0);
    108.     }
    109.     public void SetVelocityZero(){
    110.         rb.velocity = Vector2.zero;
    111.     }
    112.     /*void AccelerometerMove(){
    113.         float x = Input.acceleration.x;
    114.  
    115.         Debug.Log (" X: " + x);
    116.  
    117.         if (x < -0.1f) {
    118.             MoveLeft ();
    119.         } else if (x > 0.1f) {
    120.             MoveRight ();
    121.         }
    122.         else {
    123.             SetVelocityZero();
    124.         }
    125.  
    126.     }*/
    127. }
    128.  
    The object that script is attached to has a rigidbody and a box collider , the objects it's colliding with have box colliders.

    EDIT: Just tested again and even if I just use the accelerometer and not touch the screen , the gameobject still goes bezerk and starts ignoring collisions. ( Keep in mind the script worked fine in the APK when I didn'tt have the Transform.Translate code , and just the Accelerometer code.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    If you are using Translate you probably want to mark the rigidbody as isKinematic because otherwise you might be "driving" the object into other colliders, causing the physics system to react. Is your RB marked as kinematic?
     
  3. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Thanks for the reply man. My object isn't kinematic , it has interpolate on and continuous collision. If I make it kinematic , wouldn't that mean I couldn't move it?(excuse my ignorance) I'm about to try it to see what's up.
     
  4. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    I tested it , and made the car kinematic , but then it wouldn't collide with the other objects , so I added RB2D's to them and the car just ignores the collision ( although the bouncing thing is gone).
     
  5. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Is this maybe happening because of interference with the accelerometer?
     
  6. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Anybody?
     
  7. LMan

    LMan

    Joined:
    Jun 1, 2013
    Posts:
    493
    Can you rig something up to show you the accelerometer input values? Perhaps you need to do a low pass filter to get rid of the small changes in value.

    But I doubt it.

    I think if you started having trouble with Translate, you should try using MovePosition or something that incorporates the physics engine in the change. Translating pretty much represents a teleport to the Physics engine.
     
  8. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Do you mean like the values when I tilt the phone? if so , I have that so I'll just make a quick GIF if you need it.
    My problem isn't even translate now , when my car collides with another car in the scene it literally just ignores it ( even if I take out the translate part), DESPITE it working in Unity Remote and the Editor , and I also have a RB and Box collider on my car and box colliders on all the enemies ( so I don't know what the problem is).

    Here's my scripts so you can get the full details
    (Scripts attached to my main game object)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CoinCollider : MonoBehaviour
    6. {
    7.     public UIManager ui;
    8.     public AudioManager am;
    9.     // Use this for initialization
    10.     void Start()
    11.     {
    12.    
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.  
    19.     }
    20.  
    21.     void OnCollisionEnter2D(Collision2D col)
    22.     {
    23.         if (col.gameObject.tag == "Coin")
    24.         {
    25.             ui.coinsCollected++;
    26.             am.coinSound.Play();
    27.             Destroy(col.gameObject);
    28.         }
    29.     }
    30. }
    31.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class carController : MonoBehaviour
    5. {
    6.     public float carSpeed;
    7.     public float maxPos = 2.2f;
    8.     Vector3 position;
    9.     public UIManager ui;
    10.     public AudioManager am;
    11.     public float MaxPosY = 3.69f;
    12.     public GameObject anim;
    13.     bool currntPlatformAndroid = false;
    14.     bool heldDown = true;
    15.     Rigidbody2D rb;
    16.     void Awake()
    17.     {
    18.  
    19.         rb = GetComponent<Rigidbody2D>();
    20.  
    21. #if UNITY_ANDROID
    22.         currntPlatformAndroid = true;
    23. #else
    24.                 currentPlatformAndroid = false;
    25. #endif
    26.  
    27.  
    28.         am.carSound.Play();
    29.  
    30.     }
    31.  
    32.     void Start()
    33.     {
    34.  
    35.         position = transform.position;
    36.  
    37.         if (currntPlatformAndroid == true)
    38.         {
    39.             Debug.Log("Android");
    40.         }
    41.         else
    42.         {
    43.             Debug.Log("Windows");
    44.         }
    45.  
    46.  
    47.  
    48.     }
    49.  
    50.  
    51.     void Update()
    52.     {
    53.  
    54.  
    55.         if (currntPlatformAndroid == true)
    56.         {
    57.             //android specific code
    58.             //TouchMove ();
    59.             AccelerometerMove();
    60.         }
    61.  
    62.         else
    63.         {
    64.  
    65.              position.y = Mathf.Clamp(position.y, -4.25f, 4.25f);
    66.             position.x = Mathf.Clamp(position.x, -2.5f, 2.5f);
    67.  
    68.  
    69.             transform.position = position;
    70.         }
    71.  
    72.         position = transform.position;
    73.         position.y = Mathf.Clamp(position.y, -4.25f, 4.25f);
    74.         position.x = Mathf.Clamp(position.x, -2.5f, 2.5f);
    75.  
    76.         transform.position = position;
    77.  
    78.     }
    79.  
    80.     void OnCollisionEnter2D(Collision2D col)
    81.     {
    82.  
    83.         if (col.gameObject.tag == "EnemyCar")
    84.         {
    85.             //Destroy (gameObject);
    86.             rb.freezeRotation = true;
    87.             Instantiate(anim, transform.position, transform.rotation);
    88.             am.explosionSound.Play();
    89.             Destroy(anim);
    90.             Destroy(this.gameObject);
    91.             am.carSound.Stop();
    92.             ui.gameOverorNot();
    93.  
    94.  
    95.         }
    96.     }
    97.  
    98.     void AccelerometerMove()
    99.     {
    100.  
    101.         float x = Input.acceleration.x;
    102.         Debug.Log("X = " + x);
    103.  
    104.  
    105.         if (x < -0.1f)
    106.         {
    107.             MoveLeft();
    108.         }
    109.         else if (x > 0.1f)
    110.         {
    111.             MoveRight();
    112.         }
    113.         else
    114.         {
    115.             SetVelocityZero();
    116.         }
    117.  
    118.     }
    119.  
    120.  
    121.     void TouchMove()
    122.     {
    123.  
    124.         if (Input.touchCount > 0)
    125.         {
    126.  
    127.             Touch touch = Input.GetTouch(0);
    128.  
    129.             float middle = Screen.width / 2;
    130.  
    131.             if (touch.position.x < middle && touch.phase == TouchPhase.Began)
    132.             {
    133.                 MoveLeft();
    134.             }
    135.             else if (touch.position.x > middle && touch.phase == TouchPhase.Began)
    136.             {
    137.                 MoveRight();
    138.             }
    139.  
    140.         }
    141.  
    142.         else
    143.         {
    144.             SetVelocityZero();
    145.         }
    146.  
    147.     }
    148.  
    149.  
    150.     public void MoveLeft()
    151.     {
    152.         rb.velocity = new Vector2(-carSpeed, 0);
    153.     }
    154.  
    155.     public void MoveRight()
    156.     {
    157.  
    158.         rb.velocity = new Vector2(carSpeed, 0);
    159.     }
    160.  
    161.     public void SetVelocityZero()
    162.     {
    163.         rb.velocity = Vector2.zero;
    164.     }
    165.  
    166.     /*void AccelerometerMove(){
    167.         float x = Input.acceleration.x;
    168.      
    169.         Debug.Log (" X: " + x);
    170.      
    171.         if (x < -0.1f) {
    172.             MoveLeft ();
    173.         } else if (x > 0.1f) {
    174.             MoveRight ();
    175.         }
    176.         else {
    177.             SetVelocityZero();
    178.         }
    179.      
    180.     }*/
    181.  
    182.  
    183.  
    184.  
    185. }
    186.  
    And you can see how it look sin the inspector ( 1st and 2nd picture)

    Here's what's attached to my enemy cars ( the ones that the main car collides with)
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyCar : MonoBehaviour {
    6.     public float speed ;
    7.     public float acceleration = 0.5f;
    8.     // Use this for initialization
    9.     void Start () {
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update () {
    14.         speed += Time.deltaTime * acceleration;
    15.  
    16.         transform.Translate(new Vector3(0, 1, 0) * speed * Time.deltaTime);
    17.      
    18.     }
    19.     void OnCollisionEnter2D(Collision2D col)
    20.     {
    21.         if (col.gameObject.tag == "Coin")
    22.         {
    23.             Destroy(col.gameObject);
    24.         }
    25.     }
    26. }
    27.  
    You can see what's attached to my enemy cars in the 3rd image.
    So , why aren't collisions occurring with some of my enemy cars?
     

    Attached Files:

    • 1.PNG
      1.PNG
      File size:
      38.6 KB
      Views:
      825
    • 2.PNG
      2.PNG
      File size:
      26.7 KB
      Views:
      803
    • 3.PNG
      3.PNG
      File size:
      30.8 KB
      Views:
      807
  9. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Supposedly this is a bug in Unity?
     
  10. LMan

    LMan

    Joined:
    Jun 1, 2013
    Posts:
    493
    Nope. People have been colliding things on android for a long time.

    I'd say you have too many variables to get a fix on the problem.

    Go back to basics and collide two cubes. When that works, match their settings to the cars and see if it fails- since they don't have scripts, you can rule out the rigid body settings.

    Then add in translation movement, and so on until you find what makes it break.
     
    tomowale and Kurt-Dekker like this.
  11. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Makes sense , I'll try this.
    EDIT : But wait , how would I collide them if I don't add scripts?
     
  12. LMan

    LMan

    Joined:
    Jun 1, 2013
    Posts:
    493
    I was thinking you'd place one cube above the other in view of the camera and see if they don't fall through each other. This would verify that your collision layers are working correctly.

    You want to rule out if the settings are responsible for the problem.

    The thing to remember is that as long as your script isn't moving the object and its only the physics, the script isn't part of the equation.
     
  13. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Ok , so far I have two cubes , one with a RB2D and a Box collider , with this script attached.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Collider : MonoBehaviour {
    6.     public float speed;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.        
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.         transform.Translate(new Vector3(1, 0, 0) * speed * Time.deltaTime);
    16.  
    17.     }
    18.     void OnCollisionEnter2D(Collision2D col)
    19.     {
    20.         if (col.gameObject.tag == "Cube")
    21.         {
    22.          
    23.             Destroy(col.gameObject);
    24.         }
    25.     }
    26. }
    The other just has the tag "Cube" and a box collider , and this works on Android. So I'm going to add the scale and stuff for my two cars and see if it still works.
     
  14. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Right , that does make sense , I'll try that now , but is what I did above still valid?
     
  15. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Well , I did it the way you suggested , and the first cube has a RB2D and Box collider , the other one just has a box collider. They don't fall through each other.. My car RB has gravity scale set to 0 while the cube has 1 , could this be a problem?
    EDIT : It works fine on Android , gonna add the Transform.Translate now.
    Transform.Translate works fine on the cube on Android
     
    Last edited: Apr 23, 2017
  16. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    I then added a much more watered down version of Car Controller to the cube

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CubeC : MonoBehaviour
    5. {
    6.     public float carSpeed;
    7.     public float maxPos = 2.2f;
    8.     Vector3 position;
    9.     public float MaxPosY = 3.69f;
    10.     public GameObject anim;
    11.     bool currntPlatformAndroid = false;
    12.     bool collided = false;
    13.     Rigidbody2D rb;
    14.     void Awake()
    15.     {
    16.  
    17.         rb = GetComponent<Rigidbody2D>();
    18.  
    19. #if UNITY_ANDROID
    20.         currntPlatformAndroid = true;
    21. #else
    22.                 currntPlatformAndroid = false;
    23. #endif
    24.  
    25.  
    26.  
    27.     }
    28.  
    29.     void Start()
    30.     {
    31.  
    32.         position = transform.position;
    33.  
    34.         if (currntPlatformAndroid == true)
    35.         {
    36.             Debug.Log("Android");
    37.         }
    38.         else
    39.         {
    40.             Debug.Log("Windows");
    41.         }
    42.  
    43.  
    44.  
    45.     }
    46.  
    47.  
    48.  
    49.  
    50.     void Update()
    51.     {
    52.         position = transform.position;
    53.         position.y = Mathf.Clamp(position.y, -4.25f, 4.25f);
    54.         position.x = Mathf.Clamp(position.x, -2.5f, 2.5f);
    55.  
    56.         transform.position = position;
    57.  
    58.  
    59.         if (currntPlatformAndroid == true)
    60.         {
    61.             //android specific code
    62.             //TouchMove ();
    63.             AccelerometerMove();
    64.         }
    65.  
    66.         else
    67.         {
    68.  
    69.             position.y = Mathf.Clamp(position.y, -4.25f, 4.25f);
    70.             position.x = Mathf.Clamp(position.x, -2.5f, 2.5f);
    71.  
    72.  
    73.             transform.position = position;
    74.         }
    75.  
    76.  
    77.  
    78.     }
    79.  
    80.  
    81.     void AccelerometerMove()
    82.     {
    83.  
    84.         float x = Input.acceleration.x;
    85.         Debug.Log("X = " + x);
    86.  
    87.  
    88.         if (x < -0.1f)
    89.         {
    90.             MoveLeft();
    91.         }
    92.         else if (x > 0.1f)
    93.         {
    94.             MoveRight();
    95.         }
    96.         else
    97.         {
    98.             SetVelocityZero();
    99.         }
    100.  
    101.     }
    102.  
    103.  
    104.     void TouchMove()
    105.     {
    106.  
    107.         if (Input.touchCount > 0)
    108.         {
    109.  
    110.             Touch touch = Input.GetTouch(0);
    111.  
    112.             float middle = Screen.width / 2;
    113.  
    114.             if (touch.position.x < middle && touch.phase == TouchPhase.Began)
    115.             {
    116.                 MoveLeft();
    117.             }
    118.             else if (touch.position.x > middle && touch.phase == TouchPhase.Began)
    119.             {
    120.                 MoveRight();
    121.             }
    122.  
    123.         }
    124.  
    125.         else
    126.         {
    127.             SetVelocityZero();
    128.         }
    129.  
    130.     }
    131.  
    132.  
    133.     public void MoveLeft()
    134.     {
    135.         rb.velocity = new Vector2(-carSpeed, 0);
    136.     }
    137.  
    138.     public void MoveRight()
    139.     {
    140.  
    141.         rb.velocity = new Vector2(carSpeed, 0);
    142.     }
    143.  
    144.     public void SetVelocityZero()
    145.     {
    146.         rb.velocity = Vector2.zero;
    147.     }
    148.  
    149.     /*void AccelerometerMove(){
    150.         float x = Input.acceleration.x;
    151.  
    152.         Debug.Log (" X: " + x);
    153.  
    154.         if (x < -0.1f) {
    155.             MoveLeft ();
    156.         } else if (x > 0.1f) {
    157.             MoveRight ();
    158.         }
    159.         else {
    160.             SetVelocityZero();
    161.         }
    162.  
    163.     }*/
    164.     void OnCollisionEnter2D(Collision2D col)
    165.     {
    166.  
    167.         if (col.gameObject.tag == "Cube")
    168.         {
    169.             Instantiate(anim, transform.position, transform.rotation);
    170.             Destroy(anim);
    171.             Destroy(this.gameObject);
    172.      
    173.  
    174.  
    175.         }
    176.     }
    177.  
    It works fine on Android , I'm gonna add the Enemy Car Script to my other cube , and if that works , then what's the problem with my cars?
    EDIT : Enemy Car works fine on my cube, so what's the problem with my cars? Is it because they are sprites and not 3D objects?
     
    Last edited: Apr 23, 2017
  17. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    I think I've found the problem , I used your method and it worked perfectly on Android. I then went back to my main scene to try and apply the same settings and it refused to work , but it does work on Start. I think the reason why it doesn't work is because of either two things , the fact that my enemy cars are instantiated
    Script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CarSpawner : MonoBehaviour {
    6.     public GameObject[] cars;
    7.     int carNo;
    8.     public float MaxPos = 2.2f;
    9.     public float delayTimer;
    10.     float timer;
    11.     // Use this for initialization
    12.     void Start () {
    13.         timer = delayTimer;
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update () {
    18.          timer -= Time.deltaTime;
    19.         if (timer <= 0) {
    20.             Vector3 carPos = new Vector3(Random.Range(-2.2f, 2.2f), transform.position.y, transform.position.z);
    21.             carNo = Random.Range(0, 7);
    22.             Instantiate(cars [carNo], carPos, transform.rotation);
    23.             timer = delayTimer;
    24.         }
    25.     }
    26. }
    OR the fact that when I press Play ( to restart the game ) all goes ary , and collision acts up. Here's the script that deals with gameover , etc.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class UIManager : MonoBehaviour {
    7.     public Text ScoreText;
    8.     public Text CoinsText;
    9.     public Text CoinsText2;
    10.     public Text HighScoreText;
    11.     public  int coinsCollected;
    12.    public  int CoinsInBalance;
    13.     int score;
    14.     static int highScore;
    15.     bool gameOver;
    16.     public Button[] buttons;
    17.     public Text[] texts;
    18.     // Use this for initialization
    19.     void Start() {
    20.         gameOver = false;
    21.         highScore = PlayerPrefs.GetInt("highscore");
    22.           score = 0;
    23.         InvokeRepeating("scoreUpdate", 1.0f, 0.5f);
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.         CoinsInBalance = PlayerPrefs.GetInt("Coins");
    30.  
    31.         ScoreText.text = "SCORE: " + score;
    32.         CoinsText.text = " COINS: " + coinsCollected;
    33.         CoinsText2.text = "COIN BANK: " + CoinsInBalance;
    34.         if (score > highScore)
    35.         {
    36.             highScore = score;
    37.             PlayerPrefs.SetInt("highscore", highScore);
    38.         }
    39.             HighScoreText.text = "HIGH SCORE: " + highScore;
    40.     }
    41.      
    42.  
    43.     public void Pause() {
    44.         if (Time.timeScale == 1)
    45.         {
    46.             Time.timeScale = 0;
    47.         }
    48.         else if (Time.timeScale == 0) {
    49.             Time.timeScale = 1;
    50.         }
    51.     }
    52.     void scoreUpdate() {
    53.         if (gameOver == false) {
    54.             score += 1;
    55.         }
    56.        
    57.     }
    58.     public void gameOverorNot() {
    59.         gameOver = true;
    60.         CoinsInBalance += coinsCollected;
    61.         PlayerPrefs.SetInt("Coins", CoinsInBalance);
    62.         foreach (Button button in buttons) {
    63.             button.gameObject.SetActive(true);
    64.         }
    65.         foreach (Text text in texts) {
    66.             text.gameObject.SetActive(true);
    67.         }
    68.     }
    69.     public void Replay() {
    70.         Application.LoadLevel(Application.loadedLevel);
    71.     }
    72.     public void Menu() {
    73.         Application.LoadLevel("MainMenu");
    74.     }
    75.     public void Exit() {
    76.         Application.Quit();
    77.     }
    78.  
    79.  
    80. }
    81.  
    It could also be because the enemy car's speed is changed rapidly?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyCar : MonoBehaviour {
    6.     public float speed ;
    7.     public float acceleration = 0.5f;
    8.     // Use this for initialization
    9.     void Start () {
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update () {
    14.         speed += Time.deltaTime * acceleration;
    15.  
    16.         transform.Translate(new Vector3(0, 1, 0) * speed * Time.deltaTime);
    17.        
    18.     }
    19.     void OnCollisionEnter2D(Collision2D col)
    20.     {
    21.         if (col.gameObject.tag == "Coin")
    22.         {
    23.             Destroy(col.gameObject);
    24.         }
    25.     }
    26. }
    27.  
    I think it's the second issue , considering collisions only go crazy when I press Play ( after thje player dies once)
     
    Last edited: Apr 24, 2017
  18. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    I've figured it out , I thought it was the fact that collision isn't updated. Basically , after collision , I have a button that allows the player to replay the game
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. public class UIManager : MonoBehaviour {
    6.     public Text ScoreText;
    7.     public Text CoinsText;
    8.     public Text CoinsText2;
    9.     public Text HighScoreText;
    10.     public  int coinsCollected;
    11.    public  int CoinsInBalance;
    12.     int score;
    13.     static int highScore;
    14.     bool gameOver;
    15.     public Button[] buttons;
    16.     public Text[] texts;
    17.     // Use this for initialization
    18.     void Start() {
    19.         gameOver = false;
    20.         highScore = PlayerPrefs.GetInt("highscore");
    21.           score = 0;
    22.         InvokeRepeating("scoreUpdate", 1.0f, 0.5f);
    23.     }
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         CoinsInBalance = PlayerPrefs.GetInt("Coins");
    28.         ScoreText.text = "SCORE: " + score;
    29.         CoinsText.text = " COINS: " + coinsCollected;
    30.         CoinsText2.text = "COIN BANK: " + CoinsInBalance;
    31.         if (score > highScore)
    32.         {
    33.             highScore = score;
    34.             PlayerPrefs.SetInt("highscore", highScore);
    35.         }
    36.             HighScoreText.text = "HIGH SCORE: " + highScore;
    37.     }
    38.    
    39.     public void Pause() {
    40.         if (Time.timeScale == 1)
    41.         {
    42.             Time.timeScale = 0;
    43.         }
    44.         else if (Time.timeScale == 0) {
    45.             Time.timeScale = 1;
    46.         }
    47.     }
    48.     void scoreUpdate() {
    49.         if (gameOver == false) {
    50.             score += 1;
    51.         }
    52.      
    53.     }
    54.     public void gameOverorNot() {
    55.         gameOver = true;
    56.         CoinsInBalance += coinsCollected;
    57.         PlayerPrefs.SetInt("Coins", CoinsInBalance);
    58.         foreach (Button button in buttons) {
    59.             button.gameObject.SetActive(true);
    60.         }
    61.         foreach (Text text in texts) {
    62.             text.gameObject.SetActive(true);
    63.         }
    64.     }
    65.     public void Replay() {
    66.         Application.LoadLevel(Application.loadedLevel);
    67.     }
    68.     public void Menu() {
    69.         Application.LoadLevel("MainMenu");
    70.     }
    71.     public void Exit() {
    72.         Application.Quit();
    73.     }
    74. }
    75.  
    Collision works when I first start the game , but when the player dies and I press "Replay" , it goes crazy. Here's the script that controls the enemy cars spawning , what the hell do I do to fix this?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class EnemyCar : MonoBehaviour {
    5.     public float speed ;
    6.     public float acceleration = 0.5f;
    7.     // Use this for initialization
    8.     void Start () {
    9.     }
    10.     // Update is called once per frame
    11.     void Update () {
    12.         speed += Time.deltaTime * acceleration;
    13.         transform.Translate(new Vector3(0, 1, 0) * speed * Time.deltaTime);
    14.      
    15.     }
    16.     void OnCollisionEnter2D(Collision2D col)
    17.     {
    18.         if (col.gameObject.tag == "Coin")
    19.         {
    20.             Destroy(col.gameObject);
    21.         }
    22.     }
    23. }
    24.  
    But once again , what's hilarious is that it still works fine on Editor/Remote , just not Android.lol
     
    Last edited: Apr 25, 2017
  19. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    The above is why I think this is a Unity bug ( along with various other people having the same problem) , unless there's a way to put Collision in Unity , as I don't understand why it only goes wrong when I replay my game ( meaning it's working on Start but not Update). Yet it still works fine on the Editor.
     
  20. LMan

    LMan

    Joined:
    Jun 1, 2013
    Posts:
    493
    Do basic cubes still collide after you reload the level?
     
    tomowale likes this.
  21. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    I'll try that , but I had the cubes in another scene , so we'll see.
    EDIT : By the way , the cars clearly collide after I reload the scene ( meaning they collide , but not in a good way.lol) , it just doesn't delete it , and go through with the OnCollision I declared on the script I attached. So I'll try it with the cubes.
     
  22. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Ok they seem to work when I just have the collision function on them ( and nothing else, even after I reload , doing some more tests , but when I attach the script below to it , ( same one attached to my car) is causing collision to be ignored after
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CubeC : MonoBehaviour
    5. {
    6.     public float carSpeed;
    7.     public float maxPos = 2.2f;
    8.     Vector3 position;
    9.     public float MaxPosY = 3.69f;
    10.     public GameObject anim;
    11.     bool currntPlatformAndroid = false;
    12.     bool collided = false;
    13.     public UIManager ui;
    14.     public AudioManager am;
    15.     Rigidbody2D rb;
    16.     void Awake()
    17.     {
    18.  
    19.         rb = GetComponent<Rigidbody2D>();
    20.  
    21. #if UNITY_ANDROID
    22.         currntPlatformAndroid = true;
    23. #else
    24.                 currntPlatformAndroid = false;
    25. #endif
    26.         am.carSound.Play();
    27.  
    28.  
    29.  
    30.     }
    31.  
    32.     void Start()
    33.     {
    34.  
    35.         position = transform.position;
    36.  
    37.         if (currntPlatformAndroid == true)
    38.         {
    39.             Debug.Log("Android");
    40.         }
    41.         else
    42.         {
    43.             Debug.Log("Windows");
    44.         }
    45.  
    46.  
    47.  
    48.     }
    49.  
    50.  
    51.  
    52.  
    53.     void Update()
    54.     {
    55.         position = transform.position;
    56.         position.y = Mathf.Clamp(position.y, -4.25f, 4.25f);
    57.         position.x = Mathf.Clamp(position.x, -2.5f, 2.5f);
    58.  
    59.         transform.position = position;
    60.  
    61.  
    62.         if (currntPlatformAndroid == true)
    63.         {
    64.             //android specific code
    65.             //TouchMove ();
    66.             AccelerometerMove();
    67.         }
    68.  
    69.         else
    70.         {
    71.  
    72.             position.y = Mathf.Clamp(position.y, -4.25f, 4.25f);
    73.             position.x = Mathf.Clamp(position.x, -2.5f, 2.5f);
    74.  
    75.  
    76.             transform.position = position;
    77.         }
    78.  
    79.  
    80.  
    81.     }
    82.  
    83.  
    84.     void AccelerometerMove()
    85.     {
    86.  
    87.         float x = Input.acceleration.x;
    88.         Debug.Log("X = " + x);
    89.  
    90.  
    91.         if (x < -0.1f)
    92.         {
    93.             MoveLeft();
    94.         }
    95.         else if (x > 0.1f)
    96.         {
    97.             MoveRight();
    98.         }
    99.         else
    100.         {
    101.             SetVelocityZero();
    102.         }
    103.  
    104.     }
    105.  
    106.  
    107.     void TouchMove()
    108.     {
    109.  
    110.         if (Input.touchCount > 0)
    111.         {
    112.  
    113.             Touch touch = Input.GetTouch(0);
    114.  
    115.             float middle = Screen.width / 2;
    116.  
    117.             if (touch.position.x < middle && touch.phase == TouchPhase.Began)
    118.             {
    119.                 MoveLeft();
    120.             }
    121.             else if (touch.position.x > middle && touch.phase == TouchPhase.Began)
    122.             {
    123.                 MoveRight();
    124.             }
    125.  
    126.         }
    127.  
    128.         else
    129.         {
    130.             SetVelocityZero();
    131.         }
    132.  
    133.     }
    134.  
    135.  
    136.     public void MoveLeft()
    137.     {
    138.         rb.velocity = new Vector2(-carSpeed, 0);
    139.     }
    140.  
    141.     public void MoveRight()
    142.     {
    143.  
    144.         rb.velocity = new Vector2(carSpeed, 0);
    145.     }
    146.  
    147.     public void SetVelocityZero()
    148.     {
    149.         rb.velocity = Vector2.zero;
    150.     }
    151.  
    152.     /*void AccelerometerMove(){
    153.         float x = Input.acceleration.x;
    154.  
    155.         Debug.Log (" X: " + x);
    156.  
    157.         if (x < -0.1f) {
    158.             MoveLeft ();
    159.         } else if (x > 0.1f) {
    160.             MoveRight ();
    161.         }
    162.         else {
    163.             SetVelocityZero();
    164.         }
    165.  
    166.     }*/
    167.     void OnCollisionEnter2D(Collision2D col)
    168.     {
    169.  
    170.         if (col.gameObject.tag == "Cube")
    171.         {
    172.             Instantiate(anim, transform.position, transform.rotation);
    173.             am.explosionSound.Play();
    174.             Destroy(anim);
    175.             Destroy(this.gameObject);
    176.             am.carSound.Stop();
    177.             ui.gameOverorNot();
    178.  
    179.         }
    180.     }
    181.  
    182.  
    183. }
    184.  
    185.  
    186.  
     
  23. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    It's not a bug , Unity's support team helped me out , turns out there was an error for destroying assets that I hadn't corrected. Here is my script that deals with movement/collision for anyone that wants it.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CubeC : MonoBehaviour
    5. {
    6.     public float carSpeed;
    7.     public float maxPos = 2.2f;
    8.     Vector3 position;
    9.     public float MaxPosY = 3.69f;
    10.     public GameObject anim;
    11.     bool currntPlatformAndroid = false;
    12.     bool collided = false;
    13.     public UIManager ui;
    14.     public AudioManager am;
    15.     Rigidbody2D rb;
    16.     void Awake()
    17.     {
    18.  
    19.         rb = GetComponent<Rigidbody2D>();
    20.  
    21. #if UNITY_ANDROID
    22.         currntPlatformAndroid = true;
    23. #else
    24.                 currntPlatformAndroid = false;
    25. #endif
    26.         am.carSound.Play();
    27.  
    28.  
    29.  
    30.     }
    31.  
    32.     void Start()
    33.     {
    34.  
    35.         position = transform.position;
    36.  
    37.         if (currntPlatformAndroid == true)
    38.         {
    39.             Debug.Log("Android");
    40.         }
    41.         else
    42.         {
    43.             Debug.Log("Windows");
    44.         }
    45.  
    46.  
    47.  
    48.     }
    49.  
    50.  
    51.     void FixedUpdate()
    52.     {
    53.         if (Input.touchCount > 0)
    54.         {
    55.             transform.Translate(new Vector3(0, 1, 0) * carSpeed * Time.deltaTime);
    56.         }
    57.         else
    58.         {
    59.             transform.Translate(new Vector3(0, -1, 0) * carSpeed * Time.deltaTime);
    60.         }
    61.     }
    62.  
    63.     void Update()
    64.     {
    65.         position = transform.position;
    66.         position.y = Mathf.Clamp(position.y, -4.25f, 4.25f);
    67.         position.x = Mathf.Clamp(position.x, -2.5f, 2.5f);
    68.  
    69.         transform.position = position;
    70.  
    71.  
    72.  
    73.         if (currntPlatformAndroid == true)
    74.         {
    75.             //android specific code
    76.             //TouchMove ();
    77.             AccelerometerMove();
    78.         }
    79.  
    80.         else
    81.         {
    82.  
    83.             position.y = Mathf.Clamp(position.y, -4.25f, 4.25f);
    84.             position.x = Mathf.Clamp(position.x, -2.5f, 2.5f);
    85.  
    86.  
    87.             transform.position = position;
    88.         }
    89.  
    90.  
    91.  
    92.     }
    93.  
    94.  
    95.     void AccelerometerMove()
    96.     {
    97.  
    98.         float x = Input.acceleration.x;
    99.         Debug.Log("X = " + x);
    100.  
    101.  
    102.         if (x < -0.1f)
    103.         {
    104.             MoveLeft();
    105.         }
    106.         else if (x > 0.1f)
    107.         {
    108.             MoveRight();
    109.         }
    110.         else
    111.         {
    112.             SetVelocityZero();
    113.         }
    114.  
    115.     }
    116.  
    117.  
    118.     void TouchMove()
    119.     {
    120.  
    121.         if (Input.touchCount > 0)
    122.         {
    123.  
    124.             Touch touch = Input.GetTouch(0);
    125.  
    126.             float middle = Screen.width / 2;
    127.  
    128.             if (touch.position.x < middle && touch.phase == TouchPhase.Began)
    129.             {
    130.                 MoveLeft();
    131.             }
    132.             else if (touch.position.x > middle && touch.phase == TouchPhase.Began)
    133.             {
    134.                 MoveRight();
    135.             }
    136.  
    137.         }
    138.  
    139.         else
    140.         {
    141.             SetVelocityZero();
    142.         }
    143.  
    144.     }
    145.  
    146.  
    147.     public void MoveLeft()
    148.     {
    149.         rb.velocity = new Vector2(-carSpeed, 0);
    150.     }
    151.  
    152.     public void MoveRight()
    153.     {
    154.  
    155.         rb.velocity = new Vector2(carSpeed, 0);
    156.     }
    157.  
    158.     public void SetVelocityZero()
    159.     {
    160.         rb.velocity = Vector2.zero;
    161.     }
    162.  
    163.     /*void AccelerometerMove(){
    164.         float x = Input.acceleration.x;
    165.  
    166.         Debug.Log (" X: " + x);
    167.  
    168.         if (x < -0.1f) {
    169.             MoveLeft ();
    170.         } else if (x > 0.1f) {
    171.             MoveRight ();
    172.         }
    173.         else {
    174.             SetVelocityZero();
    175.         }
    176.  
    177.     }*/
    178.     void OnCollisionEnter2D(Collision2D col)
    179.     {
    180.  
    181.         if (col.gameObject.tag == "Cube")
    182.         {
    183.             GameObject obj = Instantiate(anim, transform.position, transform.rotation);
    184.             Instantiate(obj);
    185.             am.explosionSound.Play();
    186.             Destroy(obj);
    187.             Destroy(this.gameObject);
    188.             am.carSound.Stop();
    189.             ui.gameOverorNot();
    190.  
    191.         }
    192.     }
    193.  
    194.  
    195. }
    196.  
    197.  
    198.  
     
    Kurt-Dekker likes this.