How can I change velocity from a different script?

Hello!

I want to reverse the velocity of Asteroids each time it is hit by an object called lasar from another script.
I know how to detect the collision, I am confused on how I can change the velocity from a different script.
Please help!

Script with the object I want to be reversed in velocity:

public class Asteroids : MonoBehaviour {

// Use this for initialization
void Start() {
    var rb = GetComponent<Rigidbody2D>();
    rb.velocity = RandomVector(-10f, 10f);
}

// Update is called once per frame
void Update () {
    

}
private Vector3 RandomVector(float min, float max) {
    var x = Random.Range(min, max);
    var y = Random.Range(min, max);
    //var z = Random.Range(min, max);
    return new Vector2(x, y);
}

}

Script I want to change it from:

void OnTriggerEnter2D(Collider2D other) {
Debug.Log(other);
if (other.transform.parent == Asteroids) {
Debug.Log(“hi”); // I want to change the velocity here
}

}

@Chessnutter
try making
the rigidbody of the asteroid a public variable
and define that rigidbody in the collision script

so it should be something like.

public asteroidscript AScript;

void Start(){

}

void FixedUpdate(){

}
OnTriggerEnter2D(Collider2D other) {
Debug.Log(other);
if (other.transform.parent == Asteroids) {
AScript.rb.velocity = new vector(x,y);

let me know if that helps

@Kaymanga_

Thanks for the help!
Unfortunately, with the following code based off what you provided I get this message:

Assets/Scripts/LasarMovement.cs(21,48): error CS1061: Type UnityEngine.Transform' does not contain a definition for rb’ and no extension method rb' of type UnityEngine.Transform’ could be found. Are you missing an assembly reference?

Do you have any idea how to fix this? And thank you again for helping. (Asteroids is also a name for a script, I assume that AScript meant Asteroid Script)

void OnTriggerEnter2D(Collider2D other) {
Debug.Log(other);
if (other.transform.parent == Asteroids) {
Asteroids.rb.velocity = -Asteroids.rb.velocity;

    }
}

First, you’re declaring the rigidbody variable in Start(); it’s supposed to be at the top of the script, otherwise, it’ll only exist in the scope of Start().

public class Asteroids : MonoBehaviour
{
    // This needs to be a public variable to be accessible from outside the script
    // Instead, keep it private and create a property to only read the Rigidbody2d
    private Rigidbody2D rb2d;

    // Access this property to manipulate the Rigidbody2d outside of the script
    public Rigidbody2D Rb2d
    {
        get { return rb2d; }
    }

    void Awake()
    {
        // Set the reference to the Rigidbody2D component
        rb2d = GetComponent<Rigidbody2D>();
    }

    void Start()
    {
        rb2d.velocity = RandomVector( -10f, 10f );
    }

    private Vector3 RandomVector( float min, float max )
    {
        var x = Random.Range(min, max);
        vary = Random.Range(min, max);
        return new Vector2( x, y );
    }
}

Then, in your other script, you have 2 options: grab a reference to the rigidbody component, or grab a reference to the Asteroid script and access its rigidbody property (that I created in the Asteroid script above). All-in-all, you need to access the rigidbody component to alter the velocity.

public class OtherScript : MonoBehaviour
{
    void OnTriggerEnter2D( Collider2D other )
    {
        Debug.Log( other );
        if ( other.transform.parent == Asteroids )
        {
            Debug.Log( "Hi" );

            // Method #1: access the velocity
            other.GetComponent<Rigidbody2D>().velocity = -other.GetComponent<Rigidbody2D>().velocity;

            // Method #2: access the velocity
            other.GetComponent<Asteroids>().Rb2d.velocity = -other.GetComponent<Asteroids>().Rb2d.velocity;    
        }
    }
}

I prefer the 2nd method because you have access to any public variables, properties, or functions in the Asteroid script, whereas, the 1st method only gives access to the rigidbody variables.

If you’re going to access more from the Asteroid script or from the Rigidbody component it’s best to cache a reference to it var asteroid = other.GetComponent<Asteroids>(); then you can easily access its variables with asteroid.Rb2d.velocity = -asteroid.Rb2d.velocity;