Problem with scripts

public class SlowGround : MonoBehaviour
{

private PlayerController SlowingSpeed;

private void Start()
{
    SlowingSpeed = GameObject.Find("Player").GetComponent<PlayerController>();
    
}

private void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.gameObject.CompareTag("Player"))
    {
        SlowingSpeed.speed -= 4;
    }
}
private void OnCollisionExit2D(Collision2D collision)
{
    if (collision.gameObject.CompareTag("Player"))
    {
        SlowingSpeed.speed += 4;
    }
}

}

I tried to make a ground which slows the player but I couldnt and I dont see the mistake here anywhere.
all kinds of help would be appreciated. I dont know if it actually enters the other script and takes the speed variable but I think it does. here is the playerController script too. (too simple just move left and right)

public class PlayerController : MonoBehaviour
{

public float speed;

public float jumpForce;

public Transform GroundCheck;

public float RadiusCheck;

bool isGrounded;

Rigidbody2D rb2d;

float PlayerInput;

void Start()
{
    rb2d = GetComponent<Rigidbody2D>();
}

void FixedUpdate()
{
    isGrounded = Physics2D.OverlapCircle(GroundCheck.position, RadiusCheck);
    PlayerInput = Input.GetAxis("Horizontal");
    rb2d.velocity = new Vector2(PlayerInput * speed, rb2d.velocity.y);
    Debug.Log(PlayerInput);
}

}

I just made some changes to your script. Changed some names to make it more readable for example. Also, i added a function for changing the value. Setting the variable directly using .speed is fine, but as you said, you don’t know if the value is even changed. Using a function helps you with that. Every time you change the value now, it will Debug a message. It will also give a message if the player isn’t found. I can’t see the problem with your code right away, but using Debug.Log will get you a long way to finding the problem.


public class SlowGround : MonoBehaviour
{
    private PlayerController playerController;

    private void Start()
    {
        //Get the PlayerController
        playerController = GameObject.Find("Player").GetComponent<PlayerController>();

        if(playerController == null)
        {
            Debug.Log("Player wasn't found!");
        }

    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        //When collision has player tag
        if (collision.gameObject.CompareTag("Player"))
        {
            //Decrease player speed
            playerController.ChangeSpeed(-4f);
        }
    }

    private void OnCollisionExit2D(Collision2D collision)
    {
        //When collision has player tag
        if (collision.gameObject.CompareTag("Player"))
        {
            //Increase player speed
            playerController.ChangeSpeed(4f);
        }
    }
}

public class PlayerController : MonoBehaviour
{
    public float speed;
    public float jumpForce;
    public Transform ground;
    public float circleRadius;
    public bool isGrounded;
    public Rigidbody2D rb2d;

    public void Start()
    {
        rb2d = GetComponent<Rigidbody2D>();
    }

    public void FixedUpdate()
    {
        float playerInput = Input.GetAxis("Horizontal");
        isGrounded = Physics2D.OverlapCircle(ground.position, circleRadius);
        rb2d.velocity = new Vector2(playerInput * speed, rb2d.velocity.y);
    }

    public void ChangeSpeed(float change)
    {
        speed += change;

        Debug.Log("Changing the speed value by: " + change);
    }


}