Search Unity

Why Doesn't My Particle System Deactivate?

Discussion in 'Scripting' started by FGPArthurVII, Jun 12, 2018.

  1. FGPArthurVII

    FGPArthurVII

    Joined:
    Jan 5, 2015
    Posts:
    106
    This script should Identify If an object is parented to the fBHand on the Hierarchy, If it is It means I am holding it in my hands, If It's not It's not. If the object I am Holding is a fire extinguisher, by pressing B It would burst/spray (setactive(true) particle system), If I release or not press it It should stop/ do not burst.

    But When I press B and, without releasing the key, release the object from my hand, If keeps Spraying forever on the ground till I grab it again press B and release the key.

    Why?
    How can I fix it?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ExtinguisherJet : MonoBehaviour
    6. {
    7.     private SteamVR_Controller.Device controller;
    8.     private Transform heldObj;
    9.     private GameObject jet;
    10.     [SerializeField] private Transform fbHand;
    11.     private string lasMessage;
    12.     private bool lookForExtinguisher ()
    13.     {
    14.         bool isThere = false;
    15.  
    16.         try
    17.         {
    18.             heldObj = fbHand.transform.GetChild(2);
    19.             isThere = true;
    20.         }
    21.         catch (System.Exception e)
    22.         {
    23.             heldObj = null;
    24.             isThere = false;
    25.         }
    26.         return isThere;
    27.     }
    28.     //--------------------------------------------//
    29.  
    30.     void Start()
    31.     {
    32.         lasMessage = "";
    33.         jet = null;
    34.     }
    35.  
    36.     private void CheckHand ()
    37.     {
    38.         if (lookForExtinguisher () && lasMessage != heldObj.name) // lasMessage is Just to Keep the console from printing the same stuff over and over
    39.         {
    40.             print ("Yo, check It out, I got some" + " " + heldObj.name);
    41.  
    42.             if (heldObj.name == "Extinguisher")
    43.             {
    44.                 jet = heldObj.GetChild (0).gameObject;
    45.             }
    46.  
    47.             lasMessage = heldObj.name;
    48.  
    49.         } else if (!lookForExtinguisher () && lasMessage != "nothing") {
    50.             print ("Ma boey! Derr's nothin' on yo hands!");
    51.             lasMessage = "nothing";
    52.         }
    53.     }
    54.  
    55.     private void Bursting ()
    56.     {
    57.         if (lookForExtinguisher ())
    58.         {
    59.             if (heldObj.name == "Extinguisher" && Input.GetKeyDown(KeyCode.B))
    60.             {
    61.                 print ("I'm Bursting");
    62.                 jet.SetActive (true);
    63.             }
    64.             /*else if (heldObj.name == "Extinguisher" && controller.GetHairTriggerDown()) //Get Hair Trigger is for the usage of the Trigger button on the Oculus Touch Controller
    65.             {
    66.                 print ("I'm Bursting");
    67.                 jet.SetActive(true);
    68.             }*/
    69.             else if (Input.GetKeyUp(KeyCode.B) /*&& controller.GetHairTriggerUp()*/ && jet != null)
    70.             {
    71.                 jet.SetActive(false);
    72.             }
    73.         }
    74.     }
    75.  
    76.     void Update ()
    77.     {
    78.         CheckHand ();
    79.         Bursting ();
    80.     }
    81. }
    Thanks;
    Arthur
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Well, make sure your drop code is unparenting the object...

    But honestly, this is not great code. Why check if it's parented every frame?

    You should know when an object is dropped, and turn off the particle at that point. Otherwise, your object is checking all the time if it's in the hand or not, even when it's just mounted on the wall or in the inventory or something.