Search Unity

IF Statements - Unity Learn

Discussion in 'Getting Started' started by sixdux8, Nov 18, 2019.

  1. sixdux8

    sixdux8

    Joined:
    Nov 13, 2019
    Posts:
    15
    Hello,

    I am doing the scripting unity lessons. On the if statements chapter I get this code from the lesson.
    I copy the script below in Microsoft Visual Studio and attach it to a cube in Unity.
    But nothing prints in the console(I press space plenty).
    The lesson does not say how to test it myslef.

    Can anyone adivse?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class coffee : MonoBehaviour
    6. {
    7.  
    8.    
    9.             float coffeeTemperature = 85.0f;
    10.             float hotLimitTemperature = 70.0f;
    11.             float coldLimitTemperature = 40.0f;
    12.  
    13.     void Update()
    14.             {
    15.                 if (Input.GetKeyDown(KeyCode.Space))
    16.                     TemperatureTest();
    17.  
    18.                 coffeeTemperature -= Time.deltaTime * 5f;
    19.             }
    20.  
    21.  
    22.             void TemperatureTest()
    23.             {
    24.                 // If the coffee's temperature is greater than the hottest drinking temperature...
    25.                 if (coffeeTemperature > hotLimitTemperature)
    26.                 {
    27.                     // ... do this.
    28.                     print("Coffee is too hot.");
    29.                 }
    30.                 // If it isn't, but the coffee temperature is less than the coldest drinking temperature...
    31.                 else if (coffeeTemperature < coldLimitTemperature)
    32.                 {
    33.                     // ... do this.
    34.                     print("Coffee is too cold.");
    35.                 }
    36.                 // If it is neither of those then...
    37.                 else
    38.                 {
    39.                     // ... do this.
    40.                     print("Coffee is just right.");
    41.                 }
    42.             }
    43.         }
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Your code looks correct to me. Make sure your code really is attached to an object in the scene, and that that object is active, and that you're pressing the Play button before you test.
     
  3. sixdux8

    sixdux8

    Joined:
    Nov 13, 2019
    Posts:
    15
    Ahhhhhh, i get it. Temperature drops as soon as I play and console logs only when i release space....
    I get it now...