Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Am I missing something glaringly obvious?

Discussion in 'Scripting' started by GreenBoxInteractive, May 30, 2015.

  1. GreenBoxInteractive

    GreenBoxInteractive

    Joined:
    Mar 23, 2015
    Posts:
    135
    I have some code, and to my knowledge the variable, "timeDelay" shouldn't decrease by time.DeltaTime. But it does. It returns the same value as nextFire.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BoxLauncher : MonoBehaviour {
    5.  
    6.  
    7.     public GameObject commonItem;
    8.     public GameObject connonItem2;
    9.     public GameObject rareItem;
    10.  
    11.     public int randomGen;
    12.  
    13.     public float timeDelay;
    14.     public float nextFire;
    15.  
    16.     void Start(){
    17.         timeDelay = 3f;
    18.         nextFire = timeDelay;
    19.     }
    20.  
    21.     void FixedUpdate(){
    22.         //randomGen = Random.Range (1, 51);
    23.  
    24.         nextFire = timeDelay -= Time.deltaTime;
    25.  
    26.         if(nextFire <= 0)
    27.         {
    28.             SpawnItem();
    29.             nextFire = timeDelay - 0.1f;
    30.         }
    31.     }
    32.  
    33.     void SpawnItem(){
    34.         Debug.Log ("Item Spawned");
    35.     }
    36. }
     
  2. GreenBoxInteractive

    GreenBoxInteractive

    Joined:
    Mar 23, 2015
    Posts:
    135
    Nevermind - I figured.
     
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    That line of code says "set timeDelay to the quantity (timeDelay - Time.deltaTime), and then set nextFire to timeDelay"

    You probably didn't mean to include the second = sign.
     
    GreenBoxInteractive likes this.