Search Unity

Can anyone help me why this code is not working!

Discussion in 'Scripting' started by SuperSteelBat, Jan 19, 2021.

  1. SuperSteelBat

    SuperSteelBat

    Joined:
    Jan 4, 2021
    Posts:
    3
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Player : MonoBehaviour
    7. {
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.  
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update
    16.     (
    17.         if (Input.GetKeyDown(KeyCode.Space))
    18.         (
    19.             Debug.Log("Space key was pressed down");
    20.         )
    21.     )
    22.  
    23.  
    24.  
    25.  
    26.  
    27.     }
    28. }
    29.  
     
  2. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,555
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player : MonoBehaviour
    6. {
    7.   // Start is called before the first frame update
    8.   void Start()
    9.   {
    10.  
    11.   }
    12.   // Update is called once per frame
    13.   void Update()
    14.   {
    15.     if (Input.GetKeyDown(KeyCode.Space))
    16.     {
    17.         Debug.Log("Space key was pressed down");
    18.     }
    19.   }
    20. }
    Edit: also you were missing () at the end of Update - added.
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Surely the compiler is telling you... but you're using parentheses
    (
    and
    )
    to open and close your method and if-statement scopes when you should be using curly brackets
    {
    and
    }
    .

    ALso you have an extra closing curly bracket on line 27.
     
    adamgolden likes this.
  4. SuperSteelBat

    SuperSteelBat

    Joined:
    Jan 4, 2021
    Posts:
    3
    Let me try this and I will see how it goes!
     
  5. SuperSteelBat

    SuperSteelBat

    Joined:
    Jan 4, 2021
    Posts:
    3
    It works! Thanks!
     
    adamgolden likes this.