Updating a speed variable from a separate script

Hey all,

I have read a lot of posts related to this topic, but still can’t get my script to function correctly. I have a cat object in a script movementCat.cs who basically just jumps on click. I have another script that spawns in “pipes” (cat trees) and I want to be able to increase the rate at which they spawn (speed) by hitting enter.

movementCat.cs below:

public pipeMove pipe

...

if (Input.GetKeyDown(KeyCode.Return))
    {
        increasePipeSpeed();
    }

}
void increasePipeSpeed()
{
        pipe.moveSpeed += .5f;
        //pipe.increaseSpeed();
        speedText.text = $"Speed: {pipe.moveSpeed}";
}

The speedText here does update with the increased speed, but the “speed” of the pipes remains the same.

 public class pipeMove : MonoBehaviour
{

    public float moveSpeed = 6;
    public float deadZone = -20;
    // Start is called before the first frame update
    void Start()
    {
        moveSpeed = 6;
    }

    // Update is called once per frame
    void Update()
    {
        Debug.Log("MS " + moveSpeed);
        transform.position = transform.position + (Vector3.left * moveSpeed) * Time.deltaTime;

        if (transform.position.x < deadZone)
        {
            Destroy(gameObject);
            //Debug.Log("Pipe deleted");
        }
    }

    public void increaseSpeed()
    {
        Debug.Log("MS increase" + moveSpeed);
        moveSpeed += .5f;
    }

I want to essentially add .5 to the “speed” in pipeMove to increase the rate at which they spawn making the cat seem to be moving faster as well.

If you want the value to be shared between all the pipes, you could use a static value.

pipeMove.moveSpeed += 0.5f;
public static float moveSpeed = 6;
1 Like

Amazing!

Just like that it works perfectly :slight_smile: TYVM!