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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

How can i fix this error?

Discussion in 'Scripting' started by IzZSuheR, Jan 27, 2018.

  1. IzZSuheR

    IzZSuheR

    Joined:
    Feb 14, 2017
    Posts:
    7
    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.EventSystems;
    6. using UltimateSurvival.GUISystem;
    7.  
    8. namespace UltimateSurvival
    9. {
    10.  
    11.     public class AmmoDragHandler2 : MonoSingleton<ItemDragHandler>
    12.     {
    13.          
    14.            protected ItemProperty.Value m_AmmoProperty;
    15.            protected ItemProperty.Value m_AmmoCapacity;
    16.            protected ItemProperty.Value m_AmmoAmount;
    17.  
    18.            [HideInInspector]
    19.            public int m_NeedAmmo = 0;
    20.  
    21.            [HideInInspector]
    22.            public bool m_singleLoad = false;
    23.          
    24.            [HideInInspector]
    25.            public SavableItem m_itemUnderPointer;
    26.  
    27.            private ItemDragHandler itemDragHandler;
    28.  
    29.            //Audio
    30.            [SerializeField]
    31.            [Tooltip("Sounds that will play when trying to reload")]
    32.            private SoundPlayer m_ReloadAudio;
    33.  
    34.                 //START
    35.             private    void Start()
    36.             {
    37.                 itemDragHandler = transform.GetComponentInParent<ItemDragHandler>();
    38.             }
    39.                
    40.             public bool LoadAmmo(Slot initialSlot, SavableItem itemUnderPointer, SavableItem m_DraggedItem)
    41.             {
    42.                 bool canLoadAmmo = (
    43.                 itemUnderPointer && m_DraggedItem &&
    44.                 m_DraggedItem.HasProperty ("Ammo Type") && itemUnderPointer.HasProperty ("Ammo Type") &&
    45.                 m_DraggedItem.GetPropertyValue ("Ammo Type").String == itemUnderPointer.GetPropertyValue ("Ammo Type").String &&
    46.                 m_DraggedItem.HasProperty ("Is Ammo") &&
    47.                 !itemUnderPointer.HasProperty ("Is Ammo") &&
    48.                 itemUnderPointer.HasProperty ("Ammo Capacity") && itemUnderPointer.HasProperty ("Ammo")
    49.             );
    50.  
    51.             bool canLoadAmmoMag = (
    52.             itemUnderPointer && m_DraggedItem &&
    53.             m_DraggedItem.HasProperty ("Ammo Type") && itemUnderPointer.HasProperty ("Ammo Type") &&
    54.             m_DraggedItem.GetPropertyValue ("Ammo Type").String == itemUnderPointer.GetPropertyValue ("Ammo Type").String &&
    55.             m_DraggedItem.HasProperty ("Is Ammo") &&
    56.             itemUnderPointer.HasProperty("Is Ammo Mag") &&
    57.             !m_DraggedItem.HasProperty ("Is Ammo Mag")
    58.             );
    59.             bool requiresMag = (
    60.                canLoadAmmo &&
    61.                itemUnderPointer.HasProperty ("Requires Ammo Mag") &&
    62.                !itemUnderPointer.HasProperty ("Is Ammo Mag")
    63.               );
    64.                  bool loadFromAmmoMag = (
    65.                  requiresMag && !canLoadAmmoMag
    66.              );
    67.  
    68.              if (canLoadAmmoMag) {
    69.            
    70.              int ammoStack = m_DraggedItem.CurrentInStack;
    71.              m_AmmoAmount = itemUnderPointer.GetPropertyValue ("Ammo");
    72.              var ammo = m_AmmoAmount.Int;
    73.              m_AmmoCapacity = itemUnderPointer.GetPropertyValue ("Ammo Capacity");
    74.              ammo.Current += ammoStack;
    75.  
    76.              if (ammoStack > 0){
    77.                 return loadAmmoItem(itemUnderPointer, m_Draggeditem,  ammoStack, InitialSlot, false, true);
    78.                  }else{
    79.                     return false;
    80.              }
    81.  
    82.              } else if (canLoadAmmo)
    83.              {
    84.  
    85.                 int ammoStack = m_DraggedItem.CurrentInStack;
    86.                 if (ammoStack > 0){
    87.  
    88.                     if (loadFromAmmoMag && itemUnderPointer.HasProperty ("Has Ammo Mag"))
    89.                     {
    90.                         if (!itemUnderPointer.GetPropertyValue ("Has Ammo Mag").Bool)
    91.                         {
    92.                             ammoStack = m_DraggedItem.GetPropertyValue ("Ammo").Int.Current;
    93.  
    94.                             var hasAmmo = itemUnderPointer.GetPropertyValue ("Has Ammo Mag");
    95.                             hasAmmo.SetValue (ItemProperty.Type.Bool, true);
    96.                             return loadAmmoItem (itemUnderPointer, m_DraggedItem, ammoStack, InitialSlot, true, true);
    97.                     }else{
    98.                         return false;
    99.                     }
    100.  
    101.                 }
    102.                  else {
    103.                     return loadAmmoItem (itemUnderPointer, m_DraggedItem, ammoStack, InitialSlot, true, false);
    104.                 }
    105.             }else{
    106.                 return false;
    107.             }
    108.         }
    109.          else {
    110.                          itemDragHandler.PutItemBack (initialSlot);
    111.                          initialSlot.Refresh ();
    112.  
    113.                          Destroy (itemDragHandler.m_DraggedItemRT.gameObject);
    114.                          itemDragHandler.m_DraggedItem = null;
    115.                          itemDragHandler.m_Dragging = false;
    116.                          return true;
    117.                      }
    118.             } else
    119.                  return false;
    120.             }
    121.         }
    122.  
    123.         public bool loadAmmoItem(SavableItem itemUnderPointer, SavableItem m_DraggedItem, int ammoStack, Slot initialSlot, bool
    124.         animateLoad, bool singleLoad){
    125.             m_singleLoad = singleLoad;
    126.             m_itemUnderPointer = itemUnderPointer
    127.             m_AmmoAmount = itemUnderPointer.GetPropertyValue ("Ammo");
    128.             var ammo = m_AmmoAmount.Int
    129.             m_AmmoCapacity = itemUnderPointer.GetPropertyValue ("Ammo Capacity");
    130.             if (m_AmmoAmount.Int.Current < m_AmmoCapacity.Int.Current) {          
    131.             int m_NeedAmmo = m_AmmoCapacity.Int.Current - m_AmmoAmount.Int.Current;
    132.             if (m_NeedAmmo >= ammoStack) {
    133.  
    134.                 ammo.Current += ammoStack;
    135.                 m_AmmoAmount.SetValue (ItemProperty.Type.Int, ammo);
    136.  
    137.                 if (animatedLoad) {
    138.                      GameController.LocalPlayer.Reload.Try (m_NeedAmmo, m_singleLoad);
    139.                 } else {
    140.                
    141.                     m_ReloadAudio.Play2D  ();
    142.                 }
    143.  
    144.                 Destroy (itemDragHandler.m_DraggedItemRT.gameObject);
    145.                 itemDragHandler.m_DraggedItem = null;
    146.                 itemDragHandler.m_Dragging = false;
    147.                 initialSlot.Refresh ();
    148.                 return true;
    149.             }
    150.             else
    151.             {
    152.                 ammo.Current += m_NeedAmmo;
    153.                 m_AmmoAmount.SetValue (ItemProperty.Type.Int, ammo);
    154.  
    155.                 int remainingAmmo= ammoStack - m_NeedAmmo;
    156.                 m_DraggedItem.CurrentInStack = remainingAmmo;
    157.      
    158.                 if (animatedLoad) {
    159.                      GameController.LocalPlayer.Reload.Try (m_NeedAmmo, m_singleLoad);
    160.                 } else {
    161.                
    162.                     m_ReloadAudio.Play2D  ();
    163.                 }
    164.  
    165.                 itemDragHandler.PutItemBack (initialSlot);
    166.                 initialSlot.Refresh ();
    167.  
    168.                 Destroy (itemDragHandler.m_DraggedItemRT.gameObject);
    169.                 itemDragHandler.m_DraggedItem = null;
    170.                 itemDragHandler.m_Dragging = false;
    171.                 initialSlot.Refresh ();
    172.                 return true;
    173.             }
    174.             else
    175.             {
    176.                 ammo.Current +=ammoStack;
    177.                 m_AmmoAmount.SetValue (ItemProperty.Type.Int, ammo);
    178.                 int remainingAmmo = ammoStack - needAmmo;
    179.                 m_DraggedItem.CurrentInStack = remainingAmmo;
    180.                    
    181.                 GameController.LocalPlayer.Reload.Try (needAmmo);
    182.  
    183.                 itemDragHandler.PutItemBack (initialSlot);
    184.                 initialSlot.Refresh ();
    185.                      
    186.                 Destroy (itemDragHandler.m_DraggedItemRT.gameObject);
    187.                 itemDragHandler.m_DraggedItem = null;
    188.                 itemDragHandler.m_Dragging = false;
    189.                 initialSlot.Refresh ();
    190.                 return true;
    191.  
    192.                 }
    193.             } else {
    194.             return false;
    195.             }
    196.         }
    197.  
    198.     }          
    199. }
     

    Attached Files:

  2. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    The indentation and spacing inconsistencies makes it really hard to follow. I think you have an extra closing curly in there. Just click on an opening curly in VS and it will highlight the matching closing curly. If a closing one isnt highlighted, you done messed up a curly.

    e.g. On line 117, you have an extra closing curly.

    Also in Visual Studio, use the keyboard combination 'Ctrl + K, D' to automatically format your code. This seriously needs to be formatted better.

    The nested ifs also make it really hard to follow.
     
  3. McDev02

    McDev02

    Joined:
    Nov 22, 2010
    Posts:
    661
    You got one "}" too much at line 118. Just as the error says...
     
  4. IzZSuheR

    IzZSuheR

    Joined:
    Feb 14, 2017
    Posts:
    7
    i removed it but 10 more errors popped out...
     
  5. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    We need to see those errors to determine what to do next. Just copy and paste them here.
     
  6. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Honestly, I'm not sure where you've got that script from. I'm even more confused about how all these errors made it into the script until you finally ended up with this version, recognizing that something went wrong.
    Though it seems to be based on some advanced package from the asset store

    To be honest once more, it's more or less a mess and looks a bit inefficient and very error prone due to all the string literals and there's probably a lot of casting (or even reflection) going on.

    The question is whether it makes any sense to tell you what's wrong with it, as it doesn't appear to be completely self-written. You need to start with something that's simplier in my opinion.

    Anyways, here are most of the issues:

    - line 77 typo: m_Draggeditem does not match parameter name m_DraggedItem
    - line 77, 96, 103: InitialSlot does not match parameter name initialSlot
    - line 118 -120: not quite obvious where this 'else' belongs to, perhaps it was meant to be removed?
    - line 126: missing a semicolon
    - line 128: missing a semicolon
    - line 137 and 158: animatedLoad doesn't match parameter name animateLoad
    - line 178 and 181: needAmmo doesn't match local variable's name m_NeedAmmo
    - one of the two large else blocks at the end of the script is definitely superfluous and should be removed
     
    Last edited: Jan 27, 2018
    TaleOf4Gamers likes this.
  7. IzZSuheR

    IzZSuheR

    Joined:
    Feb 14, 2017
    Posts:
    7
    Assets/AmmoDragHandler2.cs(116,9): error CS1519: Unexpected symbol `else' in class, struct, or interface member declaration

    So basically if i try to delete '{' this errors appear...

    -

    Assets/AmmoDragHandler2.cs(116,11): error CS9010: Primary constructor body is not allowed

    -

    Assets/AmmoDragHandler2.cs(121,9): error CS1525: Unexpected symbol `bool', expecting `class', `delegate', `enum', `interface', `partial', or `struct'

    -

    Assets/AmmoDragHandler2.cs(123,2): error CS1525: Unexpected symbol `m_singleLoad', expecting `class', `delegate', `enum', `interface', `partial', or `struct'

    -

    Assets/AmmoDragHandler2.cs(125,52): error CS1525: Unexpected symbol `(', expecting `class', `delegate', `enum', `interface', `partial', or `struct'

    -

    Assets/AmmoDragHandler2.cs(127,3): error CS1525: Unexpected symbol `m_AmmoCapacity', expecting `class', `delegate', `enum', `interface', `partial', or `struct'

    -

    Assets/AmmoDragHandler2.cs(127,54): error CS1525: Unexpected symbol `(', expecting `class', `delegate', `enum', `interface', `partial', or `struct'

    -

    Assets/AmmoDragHandler2.cs(128,32): error CS1525: Unexpected symbol `<'

    -

    Assets/AmmoDragHandler2.cs(128,60): error CS1525: Unexpected symbol `)'

    -

    Assets/AmmoDragHandler2.cs(129,10): error CS1525: Unexpected symbol `m_NeedAmmo'

    -

    Assets/AmmoDragHandler2.cs(129,50): error CS1525: Unexpected symbol `-'


    -

    Assets/AmmoDragHandler2.cs(129,76): error CS1525: Unexpected symbol `;'

    -

    Assets/AmmoDragHandler2.cs(132,4): error CS1525: Unexpected symbol `ammo'

    -

    Assets/AmmoDragHandler2.cs(132,17): error CS1525: Unexpected symbol `+='


    -

    Assets/AmmoDragHandler2.cs(133,26): error CS1525: Unexpected symbol `('

    -

    Assets/AmmoDragHandler2.cs(133,48): error CS1525: Unexpected symbol `,'

    -

    Assets/AmmoDragHandler2.cs(136,9): error CS1525: Unexpected symbol `GameController'

    -

    Assets/AmmoDragHandler2.cs(136,47): error CS1525: Unexpected symbol `('

    -

    Assets/AmmoDragHandler2.cs(137,8): error CS1514: Unexpected symbol `}', expecting `.' or `{'
     
  8. IzZSuheR

    IzZSuheR

    Joined:
    Feb 14, 2017
    Posts:
    7

    thanks but there are still errors because of that 'else' thing and the asset is Ultimate Survival i have watched tutorial on how to make the script but there are errors.. i made the script 3 times and still the same errors appear..
     
  9. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Take your original post from this thread and fix these errors. Then report which errors occur and post the corresponding version. For me, those were the only errors (besides all the errors about types and identifiers, as I don't have the asset). Perhaps I missed some.

    I still like to recommend to start with a smaller project.
     
  10. IzZSuheR

    IzZSuheR

    Joined:
    Feb 14, 2017
    Posts:
    7
    Hi, it actually fixed almost everything but there is still this error:

    -

    Assets/AmmoDragHandler2.cs(174,12): error CS1525: Unexpected symbol `else'

    -
     
  11. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    Its because it looks like you have an if/else/else statement. (Which doesnt exist)
    You cant have 2 else blocks for the same if.
    Its quite hard to suggest which to move/remove.
     
    Suddoha likes this.
  12. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Exactly what @TaleOf4Gamers said.

    We cannot tell you which one to remove, unless we take some time to actually think about the logic and have a guess about which one should be the correct branch. However, that's an issue you should be able to sort out. If you cannot do that, it's still too advanced. Perhaps listen to our advice and take a few steps back. It's not meant to be rude or anything, but this won't get you anywhere.
     
  13. IzZSuheR

    IzZSuheR

    Joined:
    Feb 14, 2017
    Posts:
    7
    Yes i understand.. Thanks for your time guys! I really appreciate it , i hope im going to fix it if not im going to listen to your advice.. Thanks!