Quantcast
Viewing all articles
Browse latest Browse all 2891

"Particle System has been destroyed but you're still trying to access it."

I'm making a first person shooter and the particle system for the muzzle flash keeps disappearing from the hierarchy every time I run the project. I've made sure that the particle system is in the inspector but when I run the project it turns into "Missing(Particle System)." What should I do? Script(Ignore how messy my code is, this is my first project): using UnityEngine; using TMPro; public class Carbineshoot : MonoBehaviour { public AudioSource Shootsound; private float rotate = 0; public float AKMDamage = 25; public Transform casingExitLocation; public GameObject casingPrefab; //bullet public GameObject bullet; public AudioSource loadSound; //bullet force public float shootForce, upwardForce; public GameObject player; //Gun stats public float timeBetweenShooting, spread, reloadTime, timeBetweenShots; public int magazineSize, bulletsPerTap; public bool allowButtonHold; int bulletsLeft, bulletsShot; //Recoil public Rigidbody playerRb; public float recoilForce; private Vector3 gunTip; //public GameObject gunTip; public AudioSource audioSource; public AudioSource audioSource2; public ParticleSystem particles; //bools bool shooting, readyToShoot, reloading; bool aiming; //Reference public Camera fpsCam; public Transform attackPoint; public AudioSource Loading; //public AudioSource SlideSound; //Graphics public GameObject muzzleFlash; public TextMeshProUGUI ammunitionDisplay; public AudioSource Empty_ClickSound; //bug fixing :D public bool allowInvoke = true; public GameObject Pistol; public GameObject Glock19; public Rigidbody casingRB; private void Awake() { //GameObject varGameObject = GameObject.FindWithTag("Player"); //then disable or enable script/component // varGameObject.GetComponent().enabled = false; GetComponent().Play("carbineEquip", 0, 0f); //make sure magazine is full bulletsLeft = magazineSize; readyToShoot = true; } void reloadingfx(){ Loading.Play(); //SlideSound.Play(); } void effects(){ if (Input.GetMouseButtonDown(0)) { audioSource.Play(); particles.Play(); }else{ particles.Stop(); } } private void Update() { if(Input.GetKeyDown(KeyCode.R)){ GetComponent().Play("carbineReload", 0, 0f); Reload(); reloadingfx(); } if(Input.GetKeyDown(KeyCode.W)){ } if(Input.GetKeyUp(KeyCode.W)){ } if(Input.GetButtonDown("Fire1")){ GetComponent().Play("carbineShoot", 0, 0f); } if(Input.GetButtonUp("Fire1")){ GetComponent().Play("carbineIdle", 0, 0f); } if(bulletsLeft <= 0){ if(Input.GetButtonDown("Fire1")){ Empty_ClickSound.Play(); } }else{ effects(); } MyInput(); //Set ammo display, if it exists :D if (ammunitionDisplay != null) ammunitionDisplay.SetText(bulletsLeft / bulletsPerTap + " / " + magazineSize / bulletsPerTap); } private void MyInput() { if(bulletsLeft == 0&&!reloading){ } if (allowButtonHold) shooting = Input.GetKey(KeyCode.Mouse0); else shooting = Input.GetKeyDown(KeyCode.Mouse0); //Reloading if (Input.GetKeyDown(KeyCode.R) && bulletsLeft < magazineSize && !reloading) { GetComponent().Play("carbineReload", 0, 0f); reloadingfx(); } //Reload automatically when trying to shoot without ammo if (Input.GetKeyDown(KeyCode.R) && bulletsLeft == 0 && !reloading) { GetComponent().Play("carbineReload", 0, 0f); } //Shooting if (readyToShoot && shooting && !reloading && bulletsLeft > 0) { //Set bullets shot to 0 bulletsShot = 0; Shoot(); } } private void Shoot() { readyToShoot = false; //Find the exact hit position using a raycast Ray ray = fpsCam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0)); //Just a ray through the middle of your current view RaycastHit hit; //check if ray hits something Vector3 targetPoint; if (Physics.Raycast(ray, out hit)) targetPoint = hit.point; else targetPoint = ray.GetPoint(75); //Just a point far away from the player //Calculate direction from attackPoint to targetPoint Vector3 directionWithoutSpread = targetPoint - attackPoint.position; //Calculate spread float x = Random.Range(-spread, spread); float y = Random.Range(-spread, spread); //Calculate new direction with spread Vector3 directionWithSpread = directionWithoutSpread + new Vector3(x, y, 0); //Just add spread to last direction //Instantiate bullet/projectile GameObject currentBullet = Instantiate(bullet, attackPoint.position, Quaternion.identity); //store instantiated bullet in currentBullet //Rotate bullet to shoot direction currentBullet.transform.forward = directionWithSpread.normalized; //Add forces to bullet currentBullet.GetComponent().AddForce(directionWithSpread.normalized * shootForce, ForceMode.Impulse); currentBullet.GetComponent().AddForce(fpsCam.transform.up * upwardForce, ForceMode.Impulse); //Instantiate muzzle flash, if you have one if (muzzleFlash != null) Instantiate(muzzleFlash, attackPoint.position, Quaternion.identity); bulletsLeft--; bulletsShot++; //Invoke resetShot function (if not already invoked), with your timeBetweenShooting if (allowInvoke) { Invoke("ResetShot", timeBetweenShooting); allowInvoke = false; //Add recoil to player (should only be called once) playerRb.AddForce(-directionWithSpread.normalized * recoilForce, ForceMode.Impulse); } //if more than one bulletsPerTap make sure to repeat shoot function if (bulletsShot < bulletsPerTap && bulletsLeft > 0) Invoke("Shoot", timeBetweenShots); } private void ResetShot() { //Allow shooting and invoking again readyToShoot = true; allowInvoke = true; } private void Reload() { Shootsound.Stop(); reloading = true; Invoke("ReloadFinished", reloadTime); //Invoke ReloadFinished function with your reloadTime as delay GetComponent().Play("carbineReload", 0, 0f); } private void ReloadFinished() { //Fill magazine bulletsLeft = magazineSize; reloading = false; } }

Viewing all articles
Browse latest Browse all 2891

Trending Articles