the name 'velocity' does not exist in the current context

Hallo, I am trying to create a PraticleSystem from scratch, I only want to use scripts.
I managed to spawn particles from a fixed position with random velocities and apply gravity and an external drag force to them. The particles work great.

Now I am trying to change their velocity ( create a custom collision with an invisible cube of edge size = 5) but I face a problem with local/global variables. The particle and rb are defined in the SpawnParticle() class. I want to use their velocity and position properties in the Update() class to check for collision each second.

Here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PrefabSpawner : MonoBehaviour
{
    public GameObject prefabParticle;

    public Vector3 spawnPoint = Vector3.zero; // Set the spawn point at (0,0,0)


    public float maxVelocity = 10f; // Set the maximum velocity in the Inspector

    public Vector3 gravity = new Vector3(0, -9.81f, 0);

    public float spawnRate = 10f; //set the spawnrate, make it public for inspector testing

    private float timeSinceLastSpawn; //

    public float lifetime = 5f; //destroy particles after some time to avoid memory deplition

    public float forceDragCoef = 0.01f;

    public GameObject particle;
    public Rigidbody rb;

    private void Start()
    {
      
    }
    void Update()
    {
      
        timeSinceLastSpawn += Time.deltaTime;

        if (timeSinceLastSpawn >= 1f/spawnRate)
        {
            timeSinceLastSpawn = 0f;
            SpawnParticle();

           if (particle.transform.position.x >= 5 || particle.transform.position.x <= 5)
            {
                rb.velocity.x = rb.veloctiy.x * (-1);
            }
            
        }
    }


    void SpawnParticle()
    {
        //create new particle

        var particle = GameObject.Instantiate(prefabParticle);
        var rb = particle.GetComponent<Rigidbody>();

        if (rb == null)
        {
            rb = particle.AddComponent<Rigidbody>(); // this fixes an error occured as the particle clones appeared empty
        }

        //spawn position
        particle.transform.position = spawnPoint;

        //spawn with random veloctiy
        particle.GetComponent<Rigidbody>().velocity = Random.insideUnitSphere * maxVelocity;

        //add gravity

        particle.GetComponent<Rigidbody>().AddForce(gravity, ForceMode.Acceleration);


        //destroy particle after some time
        Destroy(particle, lifetime);
        // add custom force of f(drag) = -k*velocity
        Vector3 forceDrag = -forceDragCoef * rb.velocity;
        rb.AddForce(forceDrag, ForceMode.Force);


        //add collision
        //BoxCollider collider = gameObject.AddComponent<BoxCollider>();
        particle.AddComponent<SphereCollider>();
        particle.GetComponent<SphereCollider>().enabled = true;
    }
}

I get the following error : the name ‘velocity’ does not exist in the current context

How can I make particle and rb objects available to be used in every class of the script ??
Thaanks !

UPDATE

I re-wrote the script an managed to create rb and particle as global objects but the if loop still is not working.

public class ParticleSpawner2 : MonoBehaviour
{
    public GameObject particlePrefab;
    public List<Rigidbody> particleRigidbodies;

    public Vector3 gravity = new Vector3(0, -9.81f, 0);
    public Vector3 spawnPoint = Vector3.zero; // Set the spawn point at (0,0,0)
    public int lifetime = 5; //destroy particles after some time to avoid memory deplition
    public int maxParticles = 10;
    public int spawnRate = 50; //set the spawnrate, make it public for inspector testing
    public int maxVelocity = 50; // Set the maximum velocity in the Inspector
    private float timeSinceLastSpawn; //
    public float forceDragCoef = 0.01f;

    void Start()
    {
        // Spawn particles using the ParticleSpawner function
        ParticleSpawner();
    }

    void Update()
    {

        //spawn for each update

        timeSinceLastSpawn += Time.deltaTime;

        if (timeSinceLastSpawn >= 1f / spawnRate)
        {
            timeSinceLastSpawn = 0f;
            ParticleSpawner();
        }
        // Change the velocity of each particle
        foreach (Rigidbody rb in particleRigidbodies)
        {
            if (rb.position.x >= 2 || rb.position.x <= -2)
            {
                rb.AddForce(new Vector3(-rb.velocity.x, rb.velocity.y, rb.velocity.z));
            }
            if (rb.position.y >= 2 || rb.position.y <= -2)
            {
                rb.AddForce(new Vector3(rb.velocity.x, -rb.velocity.y, rb.velocity.z));
            }
            if (rb.position.z >= 2 || rb.position.z <= -2)
            {
                rb.AddForce(new Vector3(rb.velocity.x, rb.velocity.y, -rb.velocity.z));
            }
            //if (rb.position.y >= 10 || rb.position.y <= -10)
            //{
            //  
            //}
            //if (rb.position.z >= 10 || rb.position.z <= -10)
            // {
            //    rb.AddForce(new Vector3(rb.velocity.x, rb.velocity.y, -rb.velocity.z));
            // }
        }

    }

    private void FixedUpdate()
    {

    }

    void ParticleSpawner()
    {
        // Spawn 10 particles
        for (int i = 0; i < maxParticles; i++)
        {


            GameObject particle = Instantiate(particlePrefab);
            Rigidbody rb = particle.GetComponent<Rigidbody>();

            if (rb == null)
            {
                rb = particle.AddComponent<Rigidbody>(); // this fixes an error occured as the particle clones appeared empty
            }

            //spawn position at (0,0,0)
            particle.transform.position = spawnPoint;

            //spawn with random velocti
            Vector3 velocity = Random.insideUnitSphere * maxVelocity;
            rb.velocity = velocity;

            //add gravity
            particle.GetComponent<Rigidbody>().AddForce(gravity, ForceMode.Acceleration);

            // add custom force of f(drag) = -k*velocity
            Vector3 forceDrag = -forceDragCoef * rb.velocity;
            rb.AddForce(forceDrag, ForceMode.Force);

            //destroy particle after some time
            Destroy(particle, lifetime);
        }
    }
}

You seem to be reading from a list called particleRigidbodies, but never actually adding anything to it.
you should add the RigidBody to that list in line 79, once you know that rb != null.
Otherwise your particleRigidbodies-list will be empty, which means that the foreach in line 34 has nothing to loop over

Hallo SF_FrankvHoof,
Thank you for your answer, i added the rb to the list and it worked !
Thank you !