Two scripts can not work together (Android)

I have created these two scripts but they can not work at the same time. In fact, when the first one works, the second one does not, although they are both active.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

public class Mevement : MonoBehaviour {

    float dirX;
    public float moveSpeed = 100f;
    Rigidbody2D rb;
    bool facingRight = true;
    Vector3 localScale;

    // Use this for initialization
    void Start () {
        localScale = transform.localScale;
        rb = GetComponent<Rigidbody2D> ();
    }

    // Update is called once per frame
    void Update () {
        dirX = CrossPlatformInputManager.GetAxis ("Horizontal");
    }

    void FixedUpdate()
    {
        rb.velocity = new Vector2 (dirX * moveSpeed, 0);
    }

    void LateUpdate()
    {       
        CheckWhereToFace ();
    }

    void CheckWhereToFace ()
    {
        if (dirX > 0)
            facingRight = true;
        else if (dirX < 0)
            facingRight = false;

        if (((facingRight) && (localScale.x < 0)) || ((!facingRight) && (localScale.x > 0)))
            localScale.x *= -1;

        transform.localScale = localScale;
    }

}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;

public class Jump : MonoBehaviour {

    [Range (1,10)]
    public float jumpVelocity;
    bool jumpRequest;
    bool ToccaTerra;
    Transform tr;

    void Start () {
        tr = GetComponent<Transform> ();
    }
       
    void Update () {
        if (CrossPlatformInputManager.GetButtonDown ("Jump")) {
            jumpRequest = true;
        }
    }

    void FixedUpdate () {
        if (jumpRequest) {
            if (tr.position.y <= -3.3) {
                print ("ok1");
                //GetComponent<Rigidbody2D> ().velocity = Vector2.up * jumpVelocity;
                GetComponent<Rigidbody2D>().AddForce(Vector2.up * jumpVelocity, ForceMode2D.Impulse);
                jumpRequest = false;           
            }   
        }
    }
}

How can I solve this problem? Thank you in advance for the answers.

You can edit this line in your first script:

rb.velocity = new Vector2 (dirX * moveSpeed, 0);

to be:

rb.velocity = new Vector2 (dirX * moveSpeed, rb.velocity.y);

You were previously overwriting the velocity y to be zero all of the time, in the first script.

The second script might need to change this line:

if (tr.position.y <= -3.3f) // added 'f' at the end

You have a somewhat unusual ‘height check’ that is explicit. Normally people check if they’re grounded, but maybe your game is unique in that sense, I don’t know.

You can also cache your Rigidbody2D in the second script, in Awake or Start and re-use it in the script, like you do in the first one.

Anyways, hope that helps some. :slight_smile:

1 Like

Hi,
with this changes it works perfectly!
I will follow your suggest about the Rigidbody2D.

Thank you for the answer!

Cool, glad I could help. Take it easy. :slight_smile:

1 Like