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. Dismiss Notice

need help with this script

Discussion in 'Scripting' started by Fawful10, Dec 20, 2018.

  1. Fawful10

    Fawful10

    Joined:
    Aug 6, 2017
    Posts:
    5
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Weapon : MonoBehaviour {
    6.  
    7.     public int bulletsPerMag = 30;
    8.     public int bulletsLeft;
    9.  
    10.     public floatfireRate = 0.1f;
    11.  
    12.         FLoat fireTimer;
    13.  
    14.  
    15.     // Use this for initialization
    16.     void Start () {
    17.        
    18.     }
    19.    
    20.     // Update is called once per frame
    21.     void Update () {
    22.  
    23.         if(Input.GetButton("Fire1"))
    24.         {
    25.             fire(); //Exdcute fire function when left mouse button is clicked
    26.        
    27.             {
    28.  
    29.                 if (fireTimer < fireRate)
    30.                     fireTimer += fireTimer.deltaTime;
    31.  
    32.  
    33.                 private void fire()
    34.           }
    35.             if (fireTimer < fireRate) return;
    36.             Debug.Log("Fired!")
    37.  
    38.  
    39.                 fireTimer = 0.0f; //Reset fire Timer
    40. }
    41.  
    42.  
    43.         {
    44. }
    45.  
    46.  
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,116
    Looks like you've got some curly braces in the wrong places.
     
  3. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,903
    Code (CSharp):
    1. public floatfireRate = 0.1f;
    Code (CSharp):
    1. FLoat fireTimer;
    Plus all the braces and method definition is in the middle of another method, but only half of it.

    My suggestion is to go back to the basics and learn what is the correct syntax of a C# script and you can start to tackle real problems after that. You're way ahead of your knowledge at the moment.
     
    Joe-Censored likes this.
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,116
    Looking at this again, it's just full of problems. As @LurkingNinjaDev said, start with the basics of C#, as you've got really trivial mistakes here:
    • `public floatfireRate` - There should be a space between "float" and "fireRate"
    • `FLoat fireTimer;` - Case matters in C#. There's no such thing as "FLoat"
    • `fireTimer += fireTimer.deltaTime;` - The float class doesn't have a "deltaTime" property. You're looking for `Time.deltaTime`
    • `private void fire()` - You followed this function declaration with a closing brace instead of an opening brace.
    • `Debug.Log("Fired!")` - Missing a semicolon at the end of this line.
    So that's all pure syntax stuff you need to learn about and fix.

    Even if you fix all of the syntax errors, the fixed code probably doesn't work the way you'd want. In your fire() method, you're returning if fireTimer is less than fireRate. This means you need to hold down the Fire button for a full 0.1 seconds to fire. If you just click Fire1 and release it quickly, you don't fire. After a few clicks you might accumulate enough in fireTimer to finally fire, but that'll feel really messy. Consider changing your code so that it fires immediately upon pressing Fire1, but doesn't fire again until enough time has passed.
     
  5. eneroth3

    eneroth3

    Joined:
    Oct 22, 2018
    Posts:
    63
    To start with I'd recommend to look over syntax and basic code styling (e.g. 4 spaces indentation, spelling Fire() with capital F if its supposed to be a function, remove some brackets etc). It may seem like nitpicking but right now it's quite hard to parse the code and understand what it is meant to do. Luckily IDE's like visual basic can help you a lot here, and underline code in red if there is an error to it, kinda like MsWord.
     
  6. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    I'm wondering if this was a take home interview coding exercise? If so, I'm generally not opposed to using all resources one has available in this type of situation. It's how I would handle a real on-the-job coding problem, ask for help!
     
    Joe-Censored and Antypodish like this.