Search Unity

Problems with programming a weapon with multiple barrels.

Discussion in 'Scripting' started by BlackBirdtechnologyes, Jun 30, 2020.

  1. BlackBirdtechnologyes

    BlackBirdtechnologyes

    Joined:
    Dec 1, 2018
    Posts:
    2
    Hi, everybody.
    Thanks in advance.
    The current problem I have with this program is this: Since the weapon has multiple barrels, specifically four, I've created four particle systems. The problem is that I need to use an array to place these particle systems in the inspector and activate them separately. The problem is that when I try to activate the particle system (particlesToActivate[2].play) I get a unity error.
    I know I could create 4 slots to input the particles, but I want to do it with the array
    I have searched for information in many blogs and videos but I can't find a solution that works for me.

    The errors:

    Assets\Levitation system\scripts\Gun.cs(31,38): error CS1061: 'ParticleSystem' does not contain a definition for 'play' and no accessible extension method 'play' accepting a first argument of type 'ParticleSystem' could be found (are you missing a using directive or an assembly reference?)

    Assets\Levitation system\scripts\Gun.cs(31,17): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement


    Here is the block of my question:

    Code (CSharp):
    1.  
    2. using System;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using System.Collections;
    6. using System.Collections.Generic;
    7.  
    8. public class Gun : MonoBehaviour {
    9.  
    10.     public Camera FpsCam;
    11.     public AudioSource laserSound;
    12.     public float Damage;
    13.     public float Range;
    14.     public float impactForce;
    15.     public float fireRate;
    16.     float nextShoot;
    17.     public ParticleSystem[] muzzleflash;
    18.     int shootNum;
    19.     int animNum;
    20.     int actAnim = 0;
    21.  
    22.     void Update ()
    23.     {
    24.         if (Input.GetMouseButton(0) && Time.time > nextShoot)
    25.         {
    26.             nextShoot = Time.time + fireRate;
    27.             Shoot();
    28.  
    29.             if(shootNum <=animNum)
    30.             {
    31.                
    32.                 muzzleflash[actAnim].play;
    33.                 actAnim++;
    34.             }
    35.             else if (shootNum > animNum)
    36.             {
    37.                 actAnim = 0;
    38.                 shootNum = 0;
    39.             }
    40.         }
    41.     }
    Here is the entire script:

    Code (CSharp):
    1.  
    2. using System;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using System.Collections;
    6. using System.Collections.Generic;
    7.  
    8. public class Gun : MonoBehaviour {
    9.  
    10.     public Camera FpsCam;
    11.     public AudioSource laserSound;
    12.     public float Damage;
    13.     public float Range;
    14.     public float impactForce;
    15.     public float fireRate;
    16.     float nextShoot;
    17.     public ParticleSystem[] muzzleflash;
    18.     int shootNum;
    19.     int animNum;
    20.     int actAnim = 0;
    21.  
    22.     void Update ()
    23.     {
    24.         if (Input.GetMouseButton(0) && Time.time > nextShoot)
    25.         {
    26.             nextShoot = Time.time + fireRate;
    27.             Shoot();
    28.  
    29.             if(shootNum <=animNum)
    30.             {
    31.                
    32.                 muzzleflash[actAnim].play;
    33.                 actAnim++;
    34.             }
    35.             else if (shootNum > animNum)
    36.             {
    37.                 actAnim = 0;
    38.                 shootNum = 0;
    39.             }
    40.         }
    41.     }
    42.  
    43.     void Shoot()
    44.     {
    45.        
    46.         laserSound.Play();
    47.         RaycastHit hit;
    48.         if(Physics.Raycast(FpsCam.transform.position, FpsCam.transform.forward,out hit, Range))
    49.         {
    50.             Debug.Log(hit.transform.name);
    51.             health health = hit.transform.GetComponent<health>();
    52.             if(health != null)
    53.             {
    54.                 health.TakeDamage(Damage);
    55.                 health.ImpactGlitch();    
    56.             }
    57.             if(hit.rigidbody != null)
    58.             {
    59.                 hit.rigidbody.AddForce(-hit.normal * impactForce);
    60.             }
    61.         }
    62.  
    63.     }
    64.  
    65.  
    66. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,748
    When you see any error like this, the first thing to go through your mind should be, "I am going to go look up the scripting API documentation for Unity ParticleSystem class."

    https://docs.unity3d.com/ScriptReference/ParticleSystem.html
     
  3. willemsenzo

    willemsenzo

    Joined:
    Nov 15, 2012
    Posts:
    585
    Try calling muzzleflash[actAnim].Play(); and not muzzleflash[actAnim].play;
     
    Joe-Censored likes this.
  4. BlackBirdtechnologyes

    BlackBirdtechnologyes

    Joined:
    Dec 1, 2018
    Posts:
    2
    Thanks a lot to both of you, the mistake was that I didn't write the play with a capital letter (Play), the rest I had tried everything.:)