List Of Materials & Random assign to Particle System

Evening folks

So fairly new to Unity & C#.

I want my character to bang into an enemy, enemy dies but leaves a puff of feathers in its wake.

So far I have

Created 5 Materials
Assigned different Sprite to each material.
There is a GameObject set as a Prefab with a Particle System attached to it.
A List has been created in C# and all materials assigned to it via the inspector.
A script that calls the Prefab.

Please ignore comments, its just my way of trying and keeping track :slight_smile:

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

public class BirdScript : MonoBehaviour
{
    private GameObject enemyBird;
    private Rigidbody2D bird;
    public float horizontalSpeed;
    private bool hitPlayer;
    private bool hitLightening;
    private Animator anim;
    private Collider2D coll;
    public Vector2 BirdParticleSpawn;



    public GameObject ParticleSystemPrefab;

    public List<Material> feathermatherial;
    public Material newmaterial;

    private void Start()
    {
        coll = GetComponent<Collider2D>();
        anim = GetComponent<Animator>();
        enemyBird = GetComponent<GameObject>();
        bird = GetComponent<Rigidbody2D>();
        bird.velocity = new Vector2(Random.Range(-6, -12), 0);
        Object.Destroy(this.gameObject, 7);

    }

    private void Update()
    {
        if (hitPlayer)
        {
            bird.velocity = new Vector2(Random.Range(4, 8), (Random.Range(-10, -16)));
            Object.Destroy(this.gameObject, 6);
        }
        else if (hitLightening)
        {
            bird.velocity = new Vector2(0, -1);
            Object.Destroy(this.gameObject, 1);
        }
        else if (PlayerScoreManager.Instance.Meters > GameManager.Instance.level3Text + 40 && PlayerScoreManager.Instance.Meters < GameManager.Instance.level4Text + 40)
        {
            bird.velocity = new Vector2(Random.Range(-5, -8), 0);
            Object.Destroy(this.gameObject, 7);
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "Player")
        {
           
            hitPlayer = true;
            PlayerInput.rb.velocity = new Vector2(-4, -1);
            PlayerHealthManager.Instance.SubtractFart(10);
            anim.SetBool("BirdHitPlayer", true);
            ParticleSpawn();
           
        }

        if (collision.gameObject.tag == "FartShot")
        {
            PlayerScoreManager.Instance.AddBird(1);
            hitPlayer = true;
            anim.SetBool("BirdShot", true);
            PlayerScoreManager.Instance.AddScore(20);
        }

        if (collision.gameObject.tag == "birddunk")
        {
            PlayerScoreManager.Instance.AddBird(1);
            PlayerScoreManager.Instance.AddScore(100);
            Dunked.ifDunked = true;
            BirdSlamDunkSpawn.playDunkedAnimation = true;
            Object.Destroy(this.gameObject, 0);
        }

        if (collision.gameObject.tag == "lightening")
        {
            anim.SetBool("BirdShocked", true);
            hitLightening = true;
            coll.enabled = false;
        }

        if (collision.gameObject.name == "lighteningBall")
        {                  
            anim.SetBool("BirdShocked", true);
            hitLightening = true;
            coll.enabled = false;
        }
    }

    public void ParticleSpawn()
    {
        //Get Current Bird Location and place in BirdXY
        float BirdX = GetComponent<Transform>().position.x;
        float BirdY = GetComponent<Transform>().position.y;

        // foreach (var item in feathermatherial)
        // {
        //     ParticleSystemPrefab.GetComponentInChildren<Material>();
        // }
        //newmaterial = Random.Equals(feathermatherial);

        //ParticleSystemPrefab.GetComponentInChildren<Material>().

        //Load Prefab on BirdXY location
        Instantiate(ParticleSystemPrefab, new Vector2(BirdX,BirdY), Quaternion.identity);

        //Play Prefab
        ParticleSystemPrefab.GetComponent<ParticleSystem>().Play();
    }
}

Any help appreciated

Regards
N

I’m sorry, I’m just not sure exactly what you’re asking here.

Try answer these questions:

  1. what are you trying to do (this you have done!)

  2. show us the code (this you have done)

  3. tell us what it is or is not currently doing: (ie., I expect feathers but I got lumps of coal, or I got nothing at all)

  4. tell us what you have tried, any other info, like errors or warnings you might see

  5. also try putting in Debug.Log() statements here and there (such as at the start of the trigger functions, in the if statements, etc.) with unique words in each one so you can see where the code might be executing. When doing this simplify your screen, such as reducing to one enemy, so you can tell what is happening

Apologies Kurt

At the Moment the Particle System works, When a Player hits a Bird in its place is left a particle system that produces clouds (this was just a test)

I would like to replace this Clouds with a Bunch of feathers.
I would like each particle to be a different feather. OR if this is not going to work, then each bird their after would be a different material for all Bunches
Particles would only be produced for 0.5 β†’ 1.5 seconds producing approx 3 particles.
Each time the particle system is called, I would like it to choose from a List of approx 10 feathers.

No need for number 5 as it currently works, the only help i need is when calling a particle system, I would like to randomly assign a material to it.

Cheers for the reply :slight_smile:

Regards
N

On any given GameObject, a ParticleSystem is just like any other component and you can get a reference to it with .GetComponent();

Using that reference you can get at sub-modules in the ParticleSystem component and modify them. Check the scripting reference (documentation for ParticleSystem) but I think the material fields you want to change is going to be in the renderer submodule of the ParticleSystem.

So in this instance would it be

.getComponent().GetComponentInChildres().Material ???

Did you check the documentation for Particle System? I am guessing this is the module you will need:

Use a sheet/flipbook where each tile is a unique sprite, tell it to start on a random frame and not play the sequence.

No need for all these materials and code.

1 Like

Cheers Sparrow, That makes alot more sense than creating lists and rnd etc :smile: Cheers