Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

What is wrong with my script?

Discussion in 'Scripting' started by Gillfish2102, Aug 24, 2019.

  1. Gillfish2102

    Gillfish2102

    Joined:
    Aug 24, 2019
    Posts:
    5
    I have been trying to put together a script for a game me and a couple others are working on, the script is for a weapon pick up, and as I was writing it I got a green squiggly line warning, I've tried fixing it by changing the formatting but nothing works, the problem is at the 'if' statment, I end it with a bracket and it tells me to add a semi colon and when I add a semi colon it gives me a warning, I am quite new to scripting and game development in general so I don't understand everything right now.

    Any help would be appreciated, thanks.



    using UnityEngine;

    public class Pickup : MonoBehaviour
    {
    public GameObject LowerarmR_end, Sword;
    public bool WeaponActive;

    // Start is called before the first frame update
    void Start()
    {
    Sword.SetActive(false);
    WeaponActive = false;

    }

    // Update is called once per frame
    void Update()
    {
    if (Input.GetKey("f"));


    }
    }
     
  2. Zoulz

    Zoulz

    Joined:
    Mar 14, 2014
    Posts:
    13
    Looks like you're missing a curly bracket. And i'd use a KeyCode instead of string to define which key you're getting.
    Code (CSharp):
    1. if (Input.GetKey(KeyCode.F))
    2. {
    3.     // Your code goes here.
    4. }
     
    Gillfish2102 likes this.
  3. Gillfish2102

    Gillfish2102

    Joined:
    Aug 24, 2019
    Posts:
    5
    So do I not put the if statement between the curly brackets? Like with your code?
     
  4. APSchmidtOfOld

    APSchmidtOfOld

    Joined:
    Aug 8, 2016
    Posts:
    4,473
    You need to use code tags so that your script is actually readable to everyone.

    If the problem is in the if statement, whatever you put in it must be followed by a semi-colon.
     
  5. Gillfish2102

    Gillfish2102

    Joined:
    Aug 24, 2019
    Posts:
    5
    Sorry, don't go on forums much, and it is the if statement, whenever I type the if statement it gives me the error/warning
     
  6. APSchmidtOfOld

    APSchmidtOfOld

    Joined:
    Aug 8, 2016
    Posts:
    4,473
    Line 34, remove the ;.
    Code (CSharp):
    1. if (Input.GetKey(KeyCode.F))
    (I just saw the ; that has nothing to do there. This is why you must use the code tags; click on the link I provided and edit your post).

    This is a beginner error; you need to learn more about C#. There are links to tutorials in my signature.
     
  7. Valjuin

    Valjuin

    Joined:
    May 22, 2019
    Posts:
    481
    Code (csharp):
    1. void Update()
    2. {
    3.    if (Input.GetKey("up"))
    4.    {
    5.        print("up arrow key is held down");
    6.    }
    7.  
    8.    if (Input.GetKey("down"))
    9.    {
    10.        print("down arrow key is held down");
    11.    }
    12. }
    Source: Unityhttps://docs.unity3d.com › ScriptReference › Input.GetKey.html

    Edit: My post crossed with that of APSchmidt above.
     
    Last edited: Aug 24, 2019