Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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

Discussion in 'Scripting' started by StrangeCroissant, Dec 18, 2022.

  1. StrangeCroissant

    StrangeCroissant

    Joined:
    Oct 12, 2022
    Posts:
    3
    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:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PrefabSpawner : MonoBehaviour
    6. {
    7.     public GameObject prefabParticle;
    8.  
    9.     public Vector3 spawnPoint = Vector3.zero; // Set the spawn point at (0,0,0)
    10.  
    11.  
    12.     public float maxVelocity = 10f; // Set the maximum velocity in the Inspector
    13.  
    14.     public Vector3 gravity = new Vector3(0, -9.81f, 0);
    15.  
    16.     public float spawnRate = 10f; //set the spawnrate, make it public for inspector testing
    17.  
    18.     private float timeSinceLastSpawn; //
    19.  
    20.     public float lifetime = 5f; //destroy particles after some time to avoid memory deplition
    21.  
    22.     public float forceDragCoef = 0.01f;
    23.  
    24.     public GameObject particle;
    25.     public Rigidbody rb;
    26.  
    27.     private void Start()
    28.     {
    29.      
    30.     }
    31.     void Update()
    32.     {
    33.      
    34.         timeSinceLastSpawn += Time.deltaTime;
    35.  
    36.         if (timeSinceLastSpawn >= 1f/spawnRate)
    37.         {
    38.             timeSinceLastSpawn = 0f;
    39.             SpawnParticle();
    40.  
    41.            if (particle.transform.position.x >= 5 || particle.transform.position.x <= 5)
    42.             {
    43.                 rb.velocity.x = rb.veloctiy.x * (-1);
    44.             }
    45.            
    46.         }
    47.     }
    48.  
    49.  
    50.     void SpawnParticle()
    51.     {
    52.         //create new particle
    53.  
    54.         var particle = GameObject.Instantiate(prefabParticle);
    55.         var rb = particle.GetComponent<Rigidbody>();
    56.  
    57.         if (rb == null)
    58.         {
    59.             rb = particle.AddComponent<Rigidbody>(); // this fixes an error occured as the particle clones appeared empty
    60.         }
    61.  
    62.         //spawn position
    63.         particle.transform.position = spawnPoint;
    64.  
    65.         //spawn with random veloctiy
    66.         particle.GetComponent<Rigidbody>().velocity = Random.insideUnitSphere * maxVelocity;
    67.  
    68.         //add gravity
    69.  
    70.         particle.GetComponent<Rigidbody>().AddForce(gravity, ForceMode.Acceleration);
    71.  
    72.  
    73.         //destroy particle after some time
    74.         Destroy(particle, lifetime);
    75.         // add custom force of f(drag) = -k*velocity
    76.         Vector3 forceDrag = -forceDragCoef * rb.velocity;
    77.         rb.AddForce(forceDrag, ForceMode.Force);
    78.  
    79.  
    80.         //add collision
    81.         //BoxCollider collider = gameObject.AddComponent<BoxCollider>();
    82.         particle.AddComponent<SphereCollider>();
    83.         particle.GetComponent<SphereCollider>().enabled = true;
    84.     }
    85. }


    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 !
     
  2. StrangeCroissant

    StrangeCroissant

    Joined:
    Oct 12, 2022
    Posts:
    3
    **UPDATE**

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


    Code (CSharp):
    1. public class ParticleSpawner2 : MonoBehaviour
    2. {
    3.     public GameObject particlePrefab;
    4.     public List<Rigidbody> particleRigidbodies;
    5.  
    6.     public Vector3 gravity = new Vector3(0, -9.81f, 0);
    7.     public Vector3 spawnPoint = Vector3.zero; // Set the spawn point at (0,0,0)
    8.     public int lifetime = 5; //destroy particles after some time to avoid memory deplition
    9.     public int maxParticles = 10;
    10.     public int spawnRate = 50; //set the spawnrate, make it public for inspector testing
    11.     public int maxVelocity = 50; // Set the maximum velocity in the Inspector
    12.     private float timeSinceLastSpawn; //
    13.     public float forceDragCoef = 0.01f;
    14.  
    15.     void Start()
    16.     {
    17.         // Spawn particles using the ParticleSpawner function
    18.         ParticleSpawner();
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.  
    24.         //spawn for each update
    25.  
    26.         timeSinceLastSpawn += Time.deltaTime;
    27.  
    28.         if (timeSinceLastSpawn >= 1f / spawnRate)
    29.         {
    30.             timeSinceLastSpawn = 0f;
    31.             ParticleSpawner();
    32.         }
    33.         // Change the velocity of each particle
    34.         foreach (Rigidbody rb in particleRigidbodies)
    35.         {
    36.             if (rb.position.x >= 2 || rb.position.x <= -2)
    37.             {
    38.                 rb.AddForce(new Vector3(-rb.velocity.x, rb.velocity.y, rb.velocity.z));
    39.             }
    40.             if (rb.position.y >= 2 || rb.position.y <= -2)
    41.             {
    42.                 rb.AddForce(new Vector3(rb.velocity.x, -rb.velocity.y, rb.velocity.z));
    43.             }
    44.             if (rb.position.z >= 2 || rb.position.z <= -2)
    45.             {
    46.                 rb.AddForce(new Vector3(rb.velocity.x, rb.velocity.y, -rb.velocity.z));
    47.             }
    48.             //if (rb.position.y >= 10 || rb.position.y <= -10)
    49.             //{
    50.             //  
    51.             //}
    52.             //if (rb.position.z >= 10 || rb.position.z <= -10)
    53.             // {
    54.             //    rb.AddForce(new Vector3(rb.velocity.x, rb.velocity.y, -rb.velocity.z));
    55.             // }
    56.         }
    57.  
    58.     }
    59.  
    60.     private void FixedUpdate()
    61.     {
    62.  
    63.     }
    64.  
    65.     void ParticleSpawner()
    66.     {
    67.         // Spawn 10 particles
    68.         for (int i = 0; i < maxParticles; i++)
    69.         {
    70.  
    71.  
    72.             GameObject particle = Instantiate(particlePrefab);
    73.             Rigidbody rb = particle.GetComponent<Rigidbody>();
    74.  
    75.             if (rb == null)
    76.             {
    77.                 rb = particle.AddComponent<Rigidbody>(); // this fixes an error occured as the particle clones appeared empty
    78.             }
    79.  
    80.             //spawn position at (0,0,0)
    81.             particle.transform.position = spawnPoint;
    82.  
    83.             //spawn with random velocti
    84.             Vector3 velocity = Random.insideUnitSphere * maxVelocity;
    85.             rb.velocity = velocity;
    86.  
    87.             //add gravity
    88.             particle.GetComponent<Rigidbody>().AddForce(gravity, ForceMode.Acceleration);
    89.  
    90.             // add custom force of f(drag) = -k*velocity
    91.             Vector3 forceDrag = -forceDragCoef * rb.velocity;
    92.             rb.AddForce(forceDrag, ForceMode.Force);
    93.  
    94.             //destroy particle after some time
    95.             Destroy(particle, lifetime);
    96.         }
    97.     }
    98. }
     
  3. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    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
     
  4. StrangeCroissant

    StrangeCroissant

    Joined:
    Oct 12, 2022
    Posts:
    3
    Hallo SF_FrankvHoof,
    Thank you for your answer, i added the rb to the list and it worked !
    Thank you !