Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Help Please! Machinegun Disappears When I Say .SetActive!

Discussion in 'Scripting' started by harrypotterindy, May 13, 2015.

  1. harrypotterindy

    harrypotterindy

    Joined:
    Dec 27, 2014
    Posts:
    15
    I am having an issue with the .SetActive function in Unity 5. When I say Machinegun.SetActive (true); in start, when I play the game it disappears. Here is all of my code for the Machinegun:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class MachinegunShooting : MonoBehaviour {
    6.  
    7.    public float coolDownTimer = 0.2f;
    8.    public Animation shootAnim;
    9.    public AudioClip shootSound;
    10.    public GameObject Machinegun;
    11.  
    12.    // Use this for initialization
    13.    void Start () {
    14.      Machinegun.SetActive (true);
    15.    }
    16.  
    17.    // Update is called once per frame
    18.    void Update () {
    19.      if (Input.GetKey(KeyCode.Mouse0)) {
    20.        Ray ray = new Ray(transform.position, transform.forward);
    21.        RaycastHit hit;
    22.        if(Physics.Raycast(ray, out hit))
    23.        {
    24.          AudioSource.PlayClipAtPoint(shootSound, hit.collider.transform.position);
    25.          GetComponent<Animation>().Play();
    26.        }
    27.      }
    28.    
    29.      if(coolDownTimer > 0) {
    30.        coolDownTimer = coolDownTimer - Time.deltaTime;
    31.        Machinegun.SetActive (false);
    32.      }
    33.    
    34.      if(coolDownTimer < 0) {
    35.        coolDownTimer = 0;
    36.        Machinegun.SetActive (true);
    37.      }
    38.  
    39.      if(coolDownTimer == 0) {
    40.        coolDownTimer = 0.2f;
    41.        Machinegun.SetActive (true);
    42.      }
    43.  
    44.  
    45.    }
    46. }
    47.  
    If you can help me out, I would VERY much appreciate it! Also, btw, if you want to help me build this zombie fps game, you can E-Mail me. E-Mail:
    harper@harpernicholson.ca
    (Btw, I am only 10 years old).
     
    Last edited: May 14, 2015
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    firstly, [ code] [ /code] tags when pasting in code to the forums, it formats it nicely.


    your issue is here:
    Code (csharp):
    1.  
    2. Machinegun.SetActive (false);
    3.  
    you deactivate the gameobject, doing that turns off everything on that game object, including the mesh renderers so it "disappears"... you really don't want to be doing fire delay with SetActive().,..


    have a look at the sample code here for a better way of doing fire delays

    http://docs.unity3d.com/ScriptReference/Time-time.html
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    should also point out that SetActive disables the scripts on the gameobject from running too (literally everything on the game object) so no further code will be executed. Usually you use SetActive from a manager script to turn on/off children/dependant scripts and components.
     
  4. harrypotterindy

    harrypotterindy

    Joined:
    Dec 27, 2014
    Posts:
    15
    Thank you LeftyRighy, but I still can't seem to understand. Do you have any suggestions of what I should change to make it work?

    - harrypotterindy
     
  5. Nitugard

    Nitugard

    Joined:
    May 10, 2015
    Posts:
    341
    As @LeftyRighty mentioned you should use Time.time to create timed events.
    Time.time is the time in seconds since the start of the game.

    Here is one way to do it:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MachinegunShooting : MonoBehaviour
    5. {
    6.     public float CoolDownTime = 0.2f;
    7.     public Animation shootAnim;
    8.     public AudioClip shootSound;
    9.     public GameObject Machinegun;
    10.  
    11.     //Cooldown
    12.     private float CoolDown;
    13.  
    14.     // Use this for initialization
    15.     void Start()
    16.     {
    17.         Machinegun.SetActive(true);
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         //CoolDown<Time.time means that current game time is greater than our cooldown time which means we can fire again
    24.         if (Input.GetKey(KeyCode.Mouse0) && CoolDown<Time.time)
    25.         {
    26.             Ray ray = new Ray(transform.position, transform.forward);
    27.             RaycastHit hit;
    28.             if (Physics.Raycast(ray, out hit))
    29.             {
    30.                 AudioSource.PlayClipAtPoint(shootSound, hit.collider.transform.position);
    31.                 GetComponent<Animation>().Play();
    32.  
    33.                 //CoolDown = current in game time + cooldown time
    34.                 CoolDown = Time.time + CoolDownTime;
    35.  
    36.             }
    37.         }
    38.     }
    39. }
    40.  

    Hope this helps! :)
     
    harrypotterindy likes this.
  6. harrypotterindy

    harrypotterindy

    Joined:
    Dec 27, 2014
    Posts:
    15
    Thank you! This worked absolutely PERFECT! If you want to help me build this Zombie FPS game, you can E-Mail at harper@harpernicholson.ca
    Just make sure to initailize who you are, tell me you want to help, and from there, we can make the game! And again, thank you!