Search Unity

timer wont go down.

Discussion in 'Scripting' started by thebest07111, Sep 21, 2014.

  1. thebest07111

    thebest07111

    Joined:
    Jan 20, 2014
    Posts:
    129
    I'm looking in this for quite a while.
    but i cant figure it out.
    here is my code
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class pylon : MonoBehaviour {
    5.  
    6.     public bool toggle;
    7.     public float Y;
    8.     public float timer;
    9.     public static bool nogeenkeerschieten = false;
    10.     bool strikecheck = false;
    11.     public float timer1;
    12.     public bool timerafloop = false;
    13.  
    14.     // Use this for initialization
    15.     void Start ()
    16.     {
    17.         timer = 5;
    18.         timer1 = 6;
    19.      
    20.     }
    21.    
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         Y = transform.rotation.y;
    26.  
    27.         if (transform.rotation.y > 0.1 || transform.rotation.y < -0.1)
    28.         {
    29.             timer -= 1 * Time.deltaTime;
    30.             if (timer <= 0)
    31.             {
    32.              
    33.                 Destroy(gameObject);
    34.                 Spawner.pylonnen -= 1;
    35.                 strikecheck = true;
    36.              
    37.              
    38.             }
    39.        
    40.          
    41.          
    42.         }
    43.  
    44.            /* if (toggle == false)
    45.             {
    46.                 Spawner.pylonnen -= 1;
    47.                 toggle = true;
    48.             }
    49.             */
    50.         /*}
    51.         if (toggle == true)
    52.         {
    53.             timer -= 1 * Time.deltaTime;
    54.             if (timer <= 0)
    55.             {
    56.                
    57.  
    58.                 //nogeenkeerschieten = true;
    59.                 Destroy(gameObject);
    60.                
    61.                 strikecheck = true;
    62.          */
    63.                 ///////////////////////////////////
    64.                 if (strikecheck == true)
    65.                 {
    66.                   timer1 -= 1 * Time.deltaTime;
    67.                    if (timer1 <=0)
    68.                     {
    69.                         //timer neit gelijk doen
    70.                         if (Spawner.pylonnen == 0)
    71.                         {
    72.                             Debug.Log("strike");
    73.                             //GUI.Box(new Rect(Screen.width - 100, 0, 100, 50), "STRIKE!!!");
    74.  
    75.                         }
    76.                         else if (Spawner.pylonnen > 0)
    77.                         {
    78.                             Debug.Log("2e trow");
    79.                             nogeenkeerschieten = true;
    80.  
    81.                         }
    82.                     }
    83.                 }
    84.             }
    85.         }
    86.  
    87.  
    but my problem is. That where is says that strikecheck needs to go to true; and then where strikecheck needs to be true. then there needs to be a timer counting down from 6 to 0 when that is happened it should calculate if you have a strike or not.
    But for some reason timer1 doesnt go down to 0 so it stays at 6;

    can somebody help?
     
  2. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    239
    It seems like your script is looking for the rotation y axis in euler, but you're accessing the quaternion y axis, they're very different. However there is a solution: instead of transform.rotation.y, do: transform.rotation.eulerAngles.y
    Hope this helps! :)
     
  3. thebest07111

    thebest07111

    Joined:
    Jan 20, 2014
    Posts:
    129
    no that part works.
    when the rotation y is greater then 0.1 and below - 0.1 it destroys the object.
    and also sets the spawner.pylonnen -=1. it also sets strikecheck to true.so the upper part of the code works. but the bottom part where i do
    Code (CSharp):
    1.  if (strikecheck == true)
    2.                 {
    3.                   timer1 -= 1 * Time.deltaTime;
    4.                    if (timer1 <=0)
    5.                     {
    6.                         //timer neit gelijk doen
    7.                         if (Spawner.pylonnen == 0)
    8.                         {
    9.                             Debug.Log("strike");
    10.                             //GUI.Box(new Rect(Screen.width - 100, 0, 100, 50), "STRIKE!!!");
    11.                         }
    12.                         else if (Spawner.pylonnen > 0)
    13.                         {
    14.                             Debug.Log("2e trow");
    15.                             nogeenkeerschieten = true;
    16.                         }
    doesnt work the timer1 stays at 6 and is not counting down
     
  4. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    So, you destroy the gameObject, then wonder why the code stops?

    Not even sure how youre viewing the object, since it should be disappearing from the hierarchy.
     
  5. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    239
    aha I just noticed that too! lol