Search Unity

Timer not working?

Discussion in 'Scripting' started by BubyMB, Jun 14, 2016.

  1. BubyMB

    BubyMB

    Joined:
    Jun 6, 2016
    Posts:
    140
    Hey guys! I am using this timer script that I have used many times and have worked, but for some reason it is not working now... Please help!
    Code (csharp):
    1.  
    2. void Update()
    3.     {
    4.         bool TimerTrue = false;
    5.         float timer = 1;
    6.         if (TimerTrue)
    7.             timer -= Time.deltaTime;
    8.         if (Input.GetKeyDown(KeyCode.Mouse0))
    9.         {
    10.  
    11.             GameObject Temporary_Bullet_Handler;
    12.             Temporary_Bullet_Handler = PhotonNetwork.Instantiate("projectile", Bullet_Emitter.transform.position, Bullet_Emitter.transform.rotation, 0) as GameObject;
    13.             Temporary_Bullet_Handler.GetComponent<BulletAttributes>().Owner = this.gameObject;
    14.             TimerTrue = true;  
    15.             Rigidbody Temporary_RigidBody;
    16.             Temporary_RigidBody = Temporary_Bullet_Handler.GetComponent<Rigidbody>();
    17.  
    18.             Temporary_RigidBody.AddForce(transform.forward * Bullet_Forward_Force);
    19.  
    20.  
    21.             if (timer < 0)
    22.             {
    23.                 PhotonNetwork.Destroy(Temporary_Bullet_Handler);
    24.                 TimerTrue = false;
    25.             }
    26.         }
    27.     }
    28.  
    The bullet successfully shoots but never despawns, I would do Destroy(Temporary_Bullet_Handler, 1.0f); but I am doing multiplayer and Photon does not have that feature for some reason
     
  2. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    373
    Put the declaration of TimerTrue outside of the function, right now it will always be false at the first if, stopping the timer counting down.
     
  3. BubyMB

    BubyMB

    Joined:
    Jun 6, 2016
    Posts:
    140
    Ah, yes didn't read over that. things as simple as that just slip over my head
    EDIT- Gonna have to redo the whole method as it is faulty
     
    Last edited: Jun 14, 2016
unityunity