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. Dismiss Notice

Question How to set inspector variable in script?

Discussion in 'Scripting' started by VentariWylde, Jul 30, 2023.

  1. VentariWylde

    VentariWylde

    Joined:
    Jun 16, 2021
    Posts:
    12
    so i am using this code for the basis of a bullet hell game -

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BulletHellSpawner : MonoBehaviour
    6. {
    7.     public int number_of_columns;
    8.     public float speed;
    9.     public Sprite texture;
    10.     public Color color;
    11.     public float lifetime;
    12.     public float firerate;
    13.     public float size;
    14.     private float angle;
    15.     public Material material;
    16.     public float spin_speed;
    17.     private float time;
    18.     private ParticleSystem[] childSystems;
    19.  
    20.     public ParticleSystem system;
    21.  
    22.     private void Awake()
    23.     {
    24.         Summon();
    25.     }
    26.  
    27.     private void FixedUpdate()
    28.     {
    29.         system = GetComponentInChildren<ParticleSystem>();
    30.  
    31.         if (system != null)
    32.         {
    33.             system.GetComponent<Renderer>().sortingLayerName = "Weapon";
    34.         }
    35.  
    36.         childSystems = GetComponentsInChildren<ParticleSystem>();
    37.         foreach (var system in childSystems)
    38.         {
    39.             system.GetComponent<Renderer>().sortingLayerName = "Weapon";
    40.             var collisionModule = system.collision;
    41.             collisionModule.enabled = true;
    42.             collisionModule.type = ParticleSystemCollisionType.World;
    43.             collisionModule.mode = ParticleSystemCollisionMode.Collision2D;
    44.             collisionModule.dampen = 1f;
    45.             collisionModule.bounce = 0f;
    46.             collisionModule.lifetimeLoss = 1f;
    47.         }
    48.  
    49.         time += Time.fixedDeltaTime;
    50.  
    51.         transform.rotation = Quaternion.Euler(0,0, time * spin_speed);
    52.     }
    53.  
    54.     void Summon()
    55.     {
    56.         angle = 360f / number_of_columns;
    57.  
    58.         for(int i = 0; i<number_of_columns; i++)
    59.         {
    60.             Material particleMaterial = material;
    61.  
    62.             var go = new GameObject("Particle System");
    63.             go.transform.Rotate(angle * i, 90, 0);
    64.             go.transform.parent = this.transform;
    65.             go.transform.position = this.transform.position;
    66.             system = go.AddComponent<ParticleSystem>();
    67.             go.GetComponent<ParticleSystemRenderer>().material = particleMaterial;
    68.             var mainModule = system.main;
    69.             mainModule.startSize = 0.5f;
    70.             mainModule.startSpeed = speed;
    71.             mainModule.maxParticles = 100000;
    72.             mainModule.simulationSpace = ParticleSystemSimulationSpace.World;  
    73.  
    74.             var emission = system.emission;
    75.             emission.enabled = false;
    76.  
    77.             var forma = system.shape;
    78.             forma.enabled = true;
    79.             forma.shapeType = ParticleSystemShapeType.Sprite;
    80.             forma.sprite = null;
    81.  
    82.             var text = system.textureSheetAnimation;
    83.             text.mode = ParticleSystemAnimationMode.Sprites;
    84.             text.AddSprite(texture);
    85.         }
    86.         InvokeRepeating("DoEmit", 0f, firerate);
    87.     }
    88.  
    89.     void DoEmit()
    90.     {
    91.         foreach(Transform child in transform)
    92.         {
    93.             system = child.GetComponent<ParticleSystem>();
    94.             var emitParams = new ParticleSystem.EmitParams();
    95.             emitParams.startColor = color;
    96.             emitParams.startSize = size;
    97.             emitParams.startLifetime = lifetime;
    98.             system.Emit(emitParams, 10);
    99.         }
    100.     }
    101. }
    and what i wanted to do was have another particle effect spawn when the original particle effect collided with something, and i was suggested to use sub-emitters which is perfect but with the way the BulletHellSpawner is set up, depending on what the number of columns variable is set to, will have that number of particle systems as child objects on the BulletHellSpawner. but to use sub-emitters i need to have emission set on then configure a burst mode to what i like then set the deathParticleEffectPrefab on the sub-emitters on all possible child objects on the BulletHellSpawner and also have the particles inherit stuff and also have the deathParticleEffectPrefab assigned as a child object on all possible particle systems.
    all this would be a piece of cake in the inspector and is easy to do in play mode but since i cant save any of that because of the dynamic particle systems i will have to do it in the code if thats even possible becasue i cant find anysort of documentation and Chatgpt is no help either.

    does anyone know how i can do this? if its even possible and if its not any suggestions on how i tackle this better?

    thanks to everyone that can help
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,834
    Man, please consider slowing down and breaking down that wall of run-on sentences into coherent thoughts.

    One, anything that is public can be adjusted by code. From your example, you're doing plenty of that already. You haven't really explained any specific thing you're trying to do, and where you're stuck.

    Two, particle systems are best for visual effects that have no actual consequence to the gameplay. You typically don't write code that reacts to particles hitting things, you just let the sparks or clouds or streaks or flakes fly wherever they want. Maybe you set collisions to make it look more pretty or more realistic, but don't expect the collisions of individual particles to drive anything in your game. Use regular game objects for anything that can affect regular game objects.

    Three, with straightforward bullets with little logic, a bullet hell simulation with a few hundred to a couple thousand bullets should not be hard on the system. Again, use regular game objects if the collisions matter to the game. But if you find you can't support the number of entities you want in your game, you may be looking at Jobs or DOTS or other parts of ECS. These are advanced computation approaches to support processing many many interactions between game objects in consistent ways.
     
    Bunny83 likes this.
  3. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
  4. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    I recommend to watch this and adapt it to the latest DOTS version.
     
    Bunny83 likes this.