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

Question I tried to do a recoil and I ended with 23 errors.

Discussion in 'Scripting' started by DalcopPriv, Jul 6, 2020.

  1. DalcopPriv

    DalcopPriv

    Joined:
    Jul 5, 2020
    Posts:
    2
    I writed this code with this tutorial and something gone wrong and I don't know what. Before adding [115,116,119,45,48] it work. Please Help

    Errors:
    Assets\data\Scripts\Weapon.cs(115,33): error CS0116: A namespace cannot directly contain members such as fields or methods
    Assets\data\Scripts\Weapon.cs(115,40): error CS1031: Type expected
    Assets\data\Scripts\Weapon.cs(115,40): error CS8124: Tuple must contain at least two elements.
    Assets\data\Scripts\Weapon.cs(115,40): error CS1026: ) expected
    Assets\data\Scripts\Weapon.cs(115,40): error CS1022: Type or namespace definition, or end-of-file expected
    Assets\data\Scripts\Weapon.cs(115,49): error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
    Assets\data\Scripts\Weapon.cs(115,62): error CS1022: Type or namespace definition, or end-of-file expected
    Assets\data\Scripts\Weapon.cs(115,63): error CS0116: A namespace cannot directly contain members such as fields or methods
    Assets\data\Scripts\Weapon.cs(115,69): error CS1022: Type or namespace definition, or end-of-file expected
    Assets\data\Scripts\Weapon.cs(116,33): error CS0116: A namespace cannot directly contain members such as fields or methods
    Assets\data\Scripts\Weapon.cs(116,42): error CS1022: Type or namespace definition, or end-of-file expected
    Assets\data\Scripts\Weapon.cs(116,86): error CS0650: Bad array declarator: To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type.
    Assets\data\Scripts\Weapon.cs(116,87): error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
    Assets\data\Scripts\Weapon.cs(116,100): error CS1002: ; expected
    Assets\data\Scripts\Weapon.cs(116,100): error CS1022: Type or namespace definition, or end-of-file expected
    Assets\data\Scripts\Weapon.cs(116,101): error CS0116: A namespace cannot directly contain members such as fields or methods
    Assets\data\Scripts\Weapon.cs(116,109): error CS1022: Type or namespace definition, or end-of-file expected
    Assets\data\Scripts\Weapon.cs(119,9): error CS0116: A namespace cannot directly contain members such as fields or methods
    Assets\data\Scripts\Weapon.cs(119,25): error CS1022: Type or namespace definition, or end-of-file expected
    Assets\data\Scripts\Weapon.cs(119,36): error CS0270: Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)
    Assets\data\Scripts\Weapon.cs(119,49): error CS1022: Type or namespace definition, or end-of-file expected
    Assets\data\Scripts\Weapon.cs(119,50): error CS0116: A namespace cannot directly contain members such as fields or methods
    Assets\data\Scripts\Weapon.cs(119,58): error CS1022: Type or namespace definition, or end-of-file expected


    and code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Collections.Specialized;
    4. using System.ComponentModel;
    5. using System.Security.Cryptography;
    6. using UnityEngine;
    7.  
    8. namespace Com.deduwa.SimpleHostile
    9. {
    10.     public class Weapon : MonoBehaviour
    11.     {
    12.         #region Variables
    13.  
    14.         public Gun[] loadout;
    15.         public Transform weaponParent;
    16.         public GameObject bulletholePrefab;
    17.         public LayerMask canBeShot;
    18.         public bool gunEnabled;
    19.  
    20.         private float currentCooldown;
    21.         private int currentIndex;
    22.         private GameObject currentWeapon;
    23.  
    24.         #endregion
    25.  
    26.         #region Monobehaviour Callbacks
    27.         void Start()
    28.         {
    29.  
    30.         }
    31.  
    32.  
    33.         void Update()
    34.         {
    35.             if (Input.GetKeyDown(KeyCode.Alpha1)) Equip(0);
    36.             if (currentWeapon != null)
    37.             {
    38.                 Aim(Input.GetMouseButton(1));
    39.                 if (Input.GetMouseButtonDown(0) && currentCooldown <= 0)
    40.                 {
    41.                     Shoot();
    42.                 }
    43.  
    44.                 //weapon position elasticity
    45.                 currentWeapon.transform.localRotation = Quaternion.Lerp(currentWeapon.transform.localRotation, Quaternion.identity, Time.deltaTime * 4f);
    46.  
    47.                 //cooldown
    48.                 if (currentCooldown > 0) currentCooldown -= Time.deltaTime;
    49.             }
    50.         }
    51.  
    52.         #endregion
    53.  
    54.         #region Private Methods
    55.         void Equip(int p_ind)
    56.         {
    57.             if (currentWeapon != null) Destroy(currentWeapon);
    58.  
    59.             currentIndex = p_ind;
    60.  
    61.             GameObject t_newWeapon = Instantiate(loadout[p_ind].prefab, weaponParent.position, weaponParent.rotation, weaponParent);
    62.             t_newWeapon.transform.localPosition = Vector3.zero;
    63.             t_newWeapon.transform.localEulerAngles = Vector3.zero;
    64.  
    65.             currentWeapon = t_newWeapon;
    66.         }
    67.  
    68.         void Aim(bool p_isAiming)
    69.         {
    70.             Transform t_anchor = currentWeapon.transform.Find("Anchor");
    71.             Transform t_state_ads = currentWeapon.transform.Find("States/ADS");
    72.             Transform t_state_hip = currentWeapon.transform.Find("States/Hip");
    73.  
    74.             if (p_isAiming)
    75.             {
    76.                 //aim
    77.                 t_anchor.position = Vector3.Lerp(t_anchor.position, t_state_ads.position, Time.deltaTime * loadout[currentIndex].aimSpeed);
    78.  
    79.             }
    80.             else
    81.             {
    82.                 //hip
    83.                 t_anchor.position = Vector3.Lerp(t_anchor.position, t_state_hip.position, Time.deltaTime * loadout[currentIndex].aimSpeed);
    84.             }
    85.  
    86.         }
    87.  
    88.         void Shoot()
    89.         {
    90.             Transform t_spawn = transform.Find("Kamery/PCam");
    91.  
    92.             //bloom
    93.             Vector3 t_bloom = t_spawn.position + t_spawn.forward * 1000f;
    94.             t_bloom += Random.Range(-loadout[currentIndex].bloom, loadout[currentIndex].bloom) * t_spawn.up;
    95.             t_bloom += Random.Range(-loadout[currentIndex].bloom, loadout[currentIndex].bloom) * t_spawn.right;
    96.             t_bloom -= t_spawn.position;
    97.             t_bloom.Normalize();
    98.  
    99.  
    100.  
    101.             //raycast
    102.  
    103.             RaycastHit t_hit = new RaycastHit();
    104.             if (Physics.Raycast(t_spawn.position, t_spawn.forward, out t_hit, 1000f, canBeShot))
    105.             {
    106.                 GameObject t_newHole = Instantiate(bulletholePrefab, t_hit.point + t_hit.normal * 0.001f, Quaternion.identity) as GameObject;
    107.                 t_newHole.transform.LookAt(t_hit.point + t_hit.normal);
    108.                 Destroy(t_newHole, 10f);
    109.             }
    110.         }
    111.     }
    112.         //gun fx
    113.      
    114.  
    115.         currentWeapon.transform.Rotate(-loadout[currentIndex].recoil, 0, 0);
    116.         currentWeapon.transform.position -= currentWeapon.transform.forward * loadout[currentIndex].kickback;
    117.  
    118.         //cooldown
    119.         currentCooldown = -loadout[currentIndex].firerate;
    120.  
    121.  
    122.     #endregion
    123. }
    124.  
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You close out the class with the closing brace on line 111. What class and method are lines 115 through 119 supposed to be in?
     
  3. DalcopPriv

    DalcopPriv

    Joined:
    Jul 5, 2020
    Posts:
    2
    nevermind, brace 110 and 111 supposed to be at the end, anyway thanks for help
     
    Joe-Censored likes this.