Search Unity

List Of Materials & Random assign to Particle System

Discussion in 'Scripting' started by nathanhawthorne12, Jun 26, 2019.

  1. nathanhawthorne12

    nathanhawthorne12

    Joined:
    Jan 2, 2019
    Posts:
    29
    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 :)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BirdScript : MonoBehaviour
    6. {
    7.     private GameObject enemyBird;
    8.     private Rigidbody2D bird;
    9.     public float horizontalSpeed;
    10.     private bool hitPlayer;
    11.     private bool hitLightening;
    12.     private Animator anim;
    13.     private Collider2D coll;
    14.     public Vector2 BirdParticleSpawn;
    15.  
    16.  
    17.  
    18.     public GameObject ParticleSystemPrefab;
    19.  
    20.     public List<Material> feathermatherial;
    21.     public Material newmaterial;
    22.  
    23.     private void Start()
    24.     {
    25.         coll = GetComponent<Collider2D>();
    26.         anim = GetComponent<Animator>();
    27.         enemyBird = GetComponent<GameObject>();
    28.         bird = GetComponent<Rigidbody2D>();
    29.         bird.velocity = new Vector2(Random.Range(-6, -12), 0);
    30.         Object.Destroy(this.gameObject, 7);
    31.  
    32.     }
    33.  
    34.     private void Update()
    35.     {
    36.         if (hitPlayer)
    37.         {
    38.             bird.velocity = new Vector2(Random.Range(4, 8), (Random.Range(-10, -16)));
    39.             Object.Destroy(this.gameObject, 6);
    40.         }
    41.         else if (hitLightening)
    42.         {
    43.             bird.velocity = new Vector2(0, -1);
    44.             Object.Destroy(this.gameObject, 1);
    45.         }
    46.         else if (PlayerScoreManager.Instance.Meters > GameManager.Instance.level3Text + 40 && PlayerScoreManager.Instance.Meters < GameManager.Instance.level4Text + 40)
    47.         {
    48.             bird.velocity = new Vector2(Random.Range(-5, -8), 0);
    49.             Object.Destroy(this.gameObject, 7);
    50.         }
    51.     }
    52.  
    53.     private void OnTriggerEnter2D(Collider2D collision)
    54.     {
    55.         if (collision.gameObject.tag == "Player")
    56.         {
    57.            
    58.             hitPlayer = true;
    59.             PlayerInput.rb.velocity = new Vector2(-4, -1);
    60.             PlayerHealthManager.Instance.SubtractFart(10);
    61.             anim.SetBool("BirdHitPlayer", true);
    62.             ParticleSpawn();
    63.            
    64.         }
    65.  
    66.         if (collision.gameObject.tag == "FartShot")
    67.         {
    68.             PlayerScoreManager.Instance.AddBird(1);
    69.             hitPlayer = true;
    70.             anim.SetBool("BirdShot", true);
    71.             PlayerScoreManager.Instance.AddScore(20);
    72.         }
    73.  
    74.         if (collision.gameObject.tag == "birddunk")
    75.         {
    76.             PlayerScoreManager.Instance.AddBird(1);
    77.             PlayerScoreManager.Instance.AddScore(100);
    78.             Dunked.ifDunked = true;
    79.             BirdSlamDunkSpawn.playDunkedAnimation = true;
    80.             Object.Destroy(this.gameObject, 0);
    81.         }
    82.  
    83.         if (collision.gameObject.tag == "lightening")
    84.         {
    85.             anim.SetBool("BirdShocked", true);
    86.             hitLightening = true;
    87.             coll.enabled = false;
    88.         }
    89.  
    90.         if (collision.gameObject.name == "lighteningBall")
    91.         {                  
    92.             anim.SetBool("BirdShocked", true);
    93.             hitLightening = true;
    94.             coll.enabled = false;
    95.         }
    96.     }
    97.  
    98.     public void ParticleSpawn()
    99.     {
    100.         //Get Current Bird Location and place in BirdXY
    101.         float BirdX = GetComponent<Transform>().position.x;
    102.         float BirdY = GetComponent<Transform>().position.y;
    103.  
    104.         // foreach (var item in feathermatherial)
    105.         // {
    106.         //     ParticleSystemPrefab.GetComponentInChildren<Material>();
    107.         // }
    108.         //newmaterial = Random.Equals(feathermatherial);
    109.  
    110.         //ParticleSystemPrefab.GetComponentInChildren<Material>().
    111.  
    112.         //Load Prefab on BirdXY location
    113.         Instantiate(ParticleSystemPrefab, new Vector2(BirdX,BirdY), Quaternion.identity);
    114.  
    115.         //Play Prefab
    116.         ParticleSystemPrefab.GetComponent<ParticleSystem>().Play();
    117.     }
    118. }
    119.  
    Any help appreciated

    Regards
    N
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,691
    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
     
  3. nathanhawthorne12

    nathanhawthorne12

    Joined:
    Jan 2, 2019
    Posts:
    29
    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<material> 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 :)

    Regards
    N
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,691
    On any given GameObject, a ParticleSystem is just like any other component and you can get a reference to it with .GetComponent<ParticleSystem>();

    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.
     
  5. nathanhawthorne12

    nathanhawthorne12

    Joined:
    Jan 2, 2019
    Posts:
    29
    So in this instance would it be

    .getComponent<ParticleSystem>().GetComponentInChildres<Renderer>().Material ???
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,691
  7. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    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.
     
    Kurt-Dekker likes this.
  8. nathanhawthorne12

    nathanhawthorne12

    Joined:
    Jan 2, 2019
    Posts:
    29
    Cheers Sparrow, That makes alot more sense than creating lists and rnd etc :D Cheers