Search Unity

C# Creating/Increment Time Variable

Discussion in 'Editor & General Support' started by Thamon, Aug 28, 2016.

  1. Thamon

    Thamon

    Joined:
    Apr 6, 2016
    Posts:
    3
    I'm trying to create a time based event but am unsure about how to create a time variable. I've done some research and the best I could find is to create a float variable that equals Time.time. I've created said variable assuming it works I'm unsure how to increment the variable. I tried the tried and true timeVariable++ but it didn't seem to work in this case (not that I expected it to). Below is what I currently have:

    Code (CSharp):
    1.     public float Shield() //Reduces enemy damage to player to 0 for 30 seconds
    2.     {
    3.         float expirationTimer = Time.time;
    4.  
    5.         while (PlayerScript.shield == true && expirationTimer <= 30.0)
    6.         {
    7.             PlayerScript.GetComponent<Renderer>().material.color = new Color(0.0f, 0.0f, 1.0f); //Converts player icon to blue showing active
    8.             Laser.damage = 0.0f;
    9.             Destroy(gameObject);
    10.             return Laser.damage;
    11.         }
    12.         return Laser.damage;
    13.     }
    I create the time variable and then jump into the while loop. I'm guessing if I were to add something like expirationTimer = Time.time; it would just keep resetting the variable. I'm not really sure how time works within programming... What I want is for the method to last 30 seconds and have damage revert to its base damage. Secondly, material color do I have to reset that when the method exists or will it exist blue only while the loop is running?
     
  2. zero_null

    zero_null

    Joined:
    Mar 11, 2014
    Posts:
    159
    did you solve this ???