Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question End of file expected?

Discussion in 'Getting Started' started by notjake85, Mar 26, 2024.

  1. notjake85

    notjake85

    Joined:
    Feb 20, 2024
    Posts:
    41
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class WeaponManager : MonoBehaviour
    6. {
    7.     public static WeaponManager Instance { get; set; }
    8.  
    9.     public List<GameObject> weaponSlots;
    10.  
    11.     public GameObject activeWeaponSlot;
    12.  
    13.     [Header("Ammo")]
    14.     public int totalARAmmo = 0;
    15.     public int totalPistolAmmo = 0;
    16.  
    17.     [Header("Throwables General")]
    18.     public float throwForce = 10f;
    19.     public GameObject throwableSpawn;
    20.     public float forceMultiplier = 0;
    21.     public float forceMultiplierLimit = 2;
    22.  
    23.     [Header("Lethals")]
    24.     public int lethalsCount = 0;
    25.     public Throwable.ThrowableType equippedLethalType;
    26.     public GameObject fragPrefab;
    27.  
    28.     private void Awake()
    29.     {
    30.         if (Instance != null && Instance != this)
    31.         {
    32.             Destroy(gameObject);
    33.         }
    34.         else
    35.         {
    36.             Instance = this;
    37.         }
    38.     }
    39.  
    40.     private void Start()
    41.     {
    42.         activeWeaponSlot = weaponSlots[0];
    43.  
    44.     }
    45.  
    46.     private void Update()
    47.     {
    48.         foreach (GameObject weaponSlot in weaponSlots)
    49.         {
    50.             if (weaponSlot == activeWeaponSlot)
    51.             {
    52.                 weaponSlot.SetActive(true);
    53.             }
    54.             else
    55.             {
    56.                 weaponSlot.SetActive(false);
    57.             }
    58.         }
    59.  
    60.         if (Input.GetKeyDown(KeyCode.Alpha1))
    61.         {
    62.             SwitchActiveSlot(0);
    63.         }
    64.         if (Input.GetKeyDown(KeyCode.Alpha2))
    65.         {
    66.             SwitchActiveSlot(1);
    67.         }
    68.  
    69.         if (Input.GetKey(KeyCode.G))
    70.         {
    71.             forceMultiplier += Time.deltaTime;
    72.  
    73.             if (forceMultiplier > forceMultiplierLimit)
    74.             {
    75.                 forceMultiplier = forceMultiplierLimit;
    76.             }
    77.         }
    78.  
    79.         if (Input.GetKeyUp(KeyCode.G))
    80.         {
    81.             if (lethalsCount > 0)
    82.             {
    83.                 ThrowLethal();
    84.             }
    85.  
    86.             forceMultiplier = 0;
    87.         }
    88.     }
    89.  
    90.     public void PickupWeapon(GameObject pickedupWeapon)
    91.     {
    92.         AddWeaponIntoActiveSlot(pickedupWeapon);
    93.     }
    94.  
    95.     private void AddWeaponIntoActiveSlot(GameObject pickedupWeapon)
    96.     {
    97.         DropCurrentWeapon(pickedupWeapon);
    98.  
    99.         pickedupWeapon.transform.SetParent(activeWeaponSlot.transform, false);
    100.  
    101.         Weapon weapon = pickedupWeapon.GetComponent<Weapon>();
    102.  
    103.         pickedupWeapon.transform.localPosition = new Vector3(weapon.spawnPosition.x, weapon.spawnPosition.y, weapon.spawnPosition.z);
    104.         pickedupWeapon.transform.localRotation = Quaternion.Euler(weapon.spawnRotation.x, weapon.spawnRotation.y, weapon.spawnRotation.z);
    105.  
    106.         weapon.isActiveWeapon = true;
    107.         weapon.animator.enabled = true;
    108.     }
    109.  
    110.     internal void PickupAmmo(AmmoBox ammo)
    111.     {
    112.         switch (ammo.ammoType)
    113.         {
    114.             case AmmoBox.AmmoType.Pistol_Ammo:
    115.                 totalPistolAmmo += ammo.ammoAmount;
    116.                 break;
    117.             case AmmoBox.AmmoType.AssaultRifle_Ammo:
    118.                 totalARAmmo += ammo.ammoAmount;
    119.                 break;
    120.         }
    121.     }
    122.  
    123.     private void DropCurrentWeapon(GameObject pickedupWeapon)
    124.     {
    125.         if (activeWeaponSlot.transform.childCount > 0)
    126.         {
    127.             var weaponToDrop = activeWeaponSlot.transform.GetChild(0).gameObject;
    128.  
    129.             weaponToDrop.GetComponent<Weapon>().isActiveWeapon = false;
    130.             weaponToDrop.GetComponent<Weapon>().animator.enabled = false;
    131.  
    132.             weaponToDrop.transform.SetParent(pickedupWeapon.transform.parent);
    133.             weaponToDrop.transform.localPosition = pickedupWeapon.transform.localPosition;
    134.             weaponToDrop.transform.localRotation = pickedupWeapon.transform.localRotation;
    135.         }
    136.     }
    137.  
    138.     public void SwitchActiveSlot(int slotNumber)
    139.     {
    140.         if (activeWeaponSlot.transform.childCount > 0)
    141.         {
    142.             Weapon currentWeapon = activeWeaponSlot.transform.GetChild(0).GetComponent<Weapon>();
    143.             currentWeapon.isActiveWeapon = false;
    144.         }
    145.  
    146.         activeWeaponSlot = weaponSlots[slotNumber];
    147.  
    148.         if (activeWeaponSlot.transform.childCount > 0)
    149.         {
    150.             Weapon newWeapon = activeWeaponSlot.transform.GetChild(0).GetComponent<Weapon>();
    151.             newWeapon.isActiveWeapon = true;
    152.         }
    153.        
    154.     }
    155.  
    156.     //Weapon sprite
    157.     internal void DecreaseTotalAmmo(int bulletsToDecrease, Weapon.WeaponModel thisWeaponModel)
    158.     {
    159.         switch (thisWeaponModel)
    160.         {
    161.             case Weapon.WeaponModel.SVA_545:
    162.                 totalARAmmo -= bulletsToDecrease;
    163.                 break;
    164.  
    165.             case Weapon.WeaponModel.FF_Magnum:
    166.                 totalPistolAmmo -= bulletsToDecrease;
    167.                 break;
    168.  
    169.             case Weapon.WeaponModel.Glock:
    170.                 totalPistolAmmo -= bulletsToDecrease;
    171.                 break;
    172.         }
    173.     }
    174.  
    175.  
    176.     //Ammo sprite
    177.     public int CheckAmmoLeftFor(Weapon.WeaponModel thisWeaponModel)
    178.     {
    179.         switch (thisWeaponModel)
    180.         {
    181.             case Weapon.WeaponModel.SVA_545:
    182.                 return totalARAmmo;
    183.                
    184.             case Weapon.WeaponModel.FF_Magnum:
    185.                 return totalPistolAmmo;
    186.  
    187.             case Weapon.WeaponModel.Glock:
    188.                 return totalPistolAmmo;
    189.  
    190.             default:
    191.                 return 0;
    192.         }
    193.     }
    194.  
    195.     #region || ---- Throwables ---- ||
    196.  
    197.     public void PickupThrowable(Throwable throwable)
    198.     {
    199.         switch (throwable.throwableType)
    200.         {
    201.             case Throwable.ThrowableType.Frag:
    202.                 PickupThrowableAsLethal(Throwable.ThrowableType.Frag);
    203.                 break;
    204.  
    205.         }
    206.     }
    207.  
    208.     private void PickupThrowableAsLethal(Throwable.ThrowableType lethal)
    209.     {
    210.         if (equippedLethalType == lethal || equippedLethalType == Throwable.ThrowableType.None)
    211.         {
    212.             equippedLethalType = lethal;
    213.  
    214.             if (lethalsCount < 3)
    215.             {
    216.                 lethalsCount <= 1;
    217.                 Destroy(InteractionManager.Instance.hoveredThrowable.gameObject);
    218.                 HUDmanager.Instance.UpdateThrowables();
    219.             }
    220.             else{
    221.                 print("Lethals limit reached.");
    222.             }
    223.         }
    224.         else
    225.         {
    226.             //Cannot pick up differnt lethal
    227.             //Option to swap lethal
    228.         }
    229.     }
    230.  
    231.     }
    232.  
    233.     private void ThrowLethal()
    234.     {
    235.         GameObject lethalPrefab = GetThrowablePrefab();
    236.  
    237.         GameObject throwable = Instantiate(lethalPrefab, throwableSpawn.transform.position, Camera.main.transform.rotation);
    238.         Rigidbody rb = throwable.GetComponent<Rigidbody>();
    239.  
    240.         rb.AddForce(Camera.main.transform.forward * (throwForce * forceMultiplier), ForceMode.Impulse);
    241.  
    242.         throwable.GetComponent<Throwable>().hasBeenThrown = true;
    243.  
    244.         lethalsCount -= 1;
    245.  
    246.         if (lethalsCount <= 0)
    247.         {
    248.             equippedLethalType = Throwable.ThrowableType.None;
    249.         }
    250.         HUDmanager.Instance.UpdateThrowables();
    251.     }
    252.  
    253.     private GameObject GetThrowablePrefab()
    254.     {
    255.         switch (equippedLethalType)
    256.         {
    257.             case Throwable.ThrowableType.Frag:
    258.                 return fragPrefab;
    259.         }
    260.  
    261.         return new();
    262.     }
    263.     #endregion
    264. }
    Not sure what is happening.
    I have 3 errors.

    Assets\Scripts\WeaponManager.cs(233,5): error CS8803: Top-level statements must precede namespace and type declarations.

    Assets\Scripts\WeaponManager.cs(233,5): error CS0106: The modifier 'private' is not valid for this item

    Assets\Scripts\WeaponManager.cs(253,5): error CS0106: The modifier 'private' is not valid for this item

    Assets\Scripts\WeaponManager.cs(264,1): error CS1022: Type or namespace definition, or end-of-file expected
     
  2. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,999
    see if you format your code, you see

    1. }

    2. }
    line 233, has 2 end braces, which means all the crap after is now not in your class, so it tells you wrote junk at that point
     
  3. notjake85

    notjake85

    Joined:
    Feb 20, 2024
    Posts:
    41
    Haha thanks man!!!
    I appreciate it.