locked
can any body help me with this code it said like this error CS1513: } expected or just fix the code for me and sent it in the forum ill copy and paste RRS feed

  • Question

  • /// DarkTreeDevelopment (2019) DarkTree FPS v1.21
    /// If you have any questions feel free to write me at email --- darktreedevelopment@gmail.com ---
    /// Thanks for purchasing my asset!
    using UnityEngine;
    using UnityEngine.UI;
    using DTInventory;
    namespace DarkTreeFPS
    {
        public class UseObjects : MonoBehaviour
        {
            [Tooltip("The distance within which you can pick up item")]
            public float distance = 1.5f;
            
            private GameObject use;
            private GameObject useCursor;
            private Text useText;
            private InputManager input;
            private DTInventory.DTInventory inventory;
            private Button useButton;
            public static bool useState;
            private WeaponManager weaponManager;
            private SoundManager soundManager;
            private PickupItem pickupItem;
            private void Start()
            {
                useCursor = GameObject.Find("UseCursor");
                if (useCursor != null)
                {
                    useText = useCursor.GetComponentInChildren<Text>();
                    useCursor.SetActive(false);
                }
                inventory = FindObjectOfType<DTInventory.DTInventory>();
                input = FindObjectOfType<InputManager>();
                weaponManager = FindObjectOfType<WeaponManager>();
                soundManager = FindObjectOfType<SoundManager>();
                pickupItem = FindObjectOfType<PickupItem>();
                if (InputManager.useMobileInput)
                {
                    useButton = GameObject.Find("UseButton").GetComponent<Button>();
                    useButton.gameObject.SetActive(false);
                }
            }
            void Update()
            {
                Pickup();
                if(InventoryManager.showInventory && InputManager.useMobileInput)
                {
                    useButton.gameObject.SetActive(false);
                }
            }
            public void Pickup()
            {
                RaycastHit hit;
                //Hit an object within pickup distance
                if (Physics.Raycast(transform.positiontransform.forwardout hitdistance))
                {
                    if (hit.collider.tag == "Item")
                    {
                        useState = true;
                        //Get an item which we want to pickup
                        use = hit.collider.gameObject;
                        useCursor.SetActive(true);
                        if (InputManager.useMobileInput)
                            useButton.gameObject.SetActive(true);
                        if (use.GetComponent<Item>())
                        {
                            useText.text = use.GetComponent<Item>().title;
                            if (!InputManager.useMobileInput)
                            {
                                if (Input.GetKeyDown(input.Use))
                                {
                                    soundManager.Pickup();
                                    inventory.AddItem(use.GetComponent<Item>());
                                    use = null;
                                }
                            }
                            if(InputManager.useMobileInput)
                            {
                                var item = use.GetComponent<Item>();
                                useButton.onClick.RemoveAllListeners();
                                useButton.onClick.AddListener() ; => { inventory.AddItem(item); });
                                use = null;
                                return;
                            }
                                
                        }
                    }else if(hit.collider.tag == "LootBox")
                    {
                        if (InputManager.useMobileInput)
                            useButton.gameObject.SetActive(true);
                        useCursor.SetActive(true);
                        useText.text = "Inspect";
                        if (InputManager.useMobileInput)
                        {
                            useButton.onClick.RemoveAllListeners();
                            useButton.onClick.AddListener(() => { pickupItem.InspectLootBox(hit.collider.GetComponent<LootBox>()); });
                            use = null;
                            return;
                        }
                    }
                    else
                    {
                        useState = false;
                        //Clear use object if there is no an object with "Item" tag
                        use = null;
                        useCursor.SetActive(false);
                        if(InputManager.useMobileInput)
                            useButton.gameObject.SetActive(false);
                        useText.text = "";
                    }
                }
                else
                {
                    useState = false;
                    if(useCursor != null)
                    useCursor.SetActive(false);
                    if (InputManager.useMobileInput)
                        useButton.gameObject.SetActive(false);
                    if (useText != null)
                    {
                        useText.text = "";
                    }
                }
            }
        }
    }
    Wednesday, July 29, 2020 4:08 PM

All replies

  • Hello,

    We can't copy and paste your code and see when a closing bracket is missing. Look for a squiggly character is in the code and add a } bracket.

    See simple example

    https://docs.microsoft.com/en-us/dotnet/csharp/misc/cs1513


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, July 29, 2020 4:28 PM
  • The compiler error tells you the line where the error occurred. For a closing curly go back to the matching open curly to identify where inside there you went wrong.

    There is far too many open/close curlies in your code to figure this out easily. Use functions to break up this logic so it is easier to find these errors. Also use the editors ability to collapse blocks of code to click on each block and collapse it to ensure your blocks line up with what you expect.


    Michael Taylor http://www.michaeltaylorp3.net

    • Proposed as answer by Naomi N Wednesday, July 29, 2020 9:26 PM
    Wednesday, July 29, 2020 7:37 PM
  •                         if(InputManager.useMobileInput)
                            {
                                var item = use.GetComponent<Item>();
                                useButton.onClick.RemoveAllListeners();
                                useButton.onClick.AddListener() ; => { inventory.AddItem(item); });
                                use = null;
                                return;
                            }

    This line looks ill formed:

    useButton.onClick.AddListener(); => { inventory.AddItem(item); });
    

    Perhaps try something similar to this instead (untested):

    useButton.onClick.AddListener(() => { inventory.AddItem(item); });
    

    - Wayne

    • Proposed as answer by Naomi N Wednesday, July 29, 2020 9:27 PM
    Wednesday, July 29, 2020 9:19 PM
  • Hi Meak1212,
    I made a test with you code and found the wrong line of code that WayneAKing has pointed.

    useButton.onClick.AddListener(); => { inventory.AddItem(item); });

    So has your problem been solved? If it is resolved, we suggest that you mark it as the answer. If not solved, what other problems did you encounter?
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 30, 2020 1:46 AM
  • i dont under stand like can u tell me where should the close bracet should be

    Thursday, July 30, 2020 1:47 AM
  • i dont under stand like can u tell me where should the close bracet should be

    Did you try changing that line as I suggested?

    Specifically, change (); to (() 

    In the case of that line there isn't actually a missing closing brace but
    the semicolon (and missing left parenthesis) cause the compiler to issue
    a message saying it is expecting to find one. 

    The extra semicolon in the middle of that statement tells the compiler that 
    it is the end of the statement, when it isn't.

    - Wayne

    Thursday, July 30, 2020 5:07 AM
  • Hi WayneAKing,
    I made a test with you code and found the wrong line of code that WayneAKing has pointed.

    Best Regards,
    Daniel Zhang


    @Daniel - 

    It isn't *my* code, it is the OP's. You should have said:

    "Hi Meak1212"

    - Wayne

    Thursday, July 30, 2020 5:13 AM
  • i dont under stand like can u tell me where should the close bracet should be

    The compiler isn't saying that there is a closing brace missing from the
    source code. It's saying that it expected to encounter one before something
    else in the code caused it to stop parsing for one. Example:

    static void Main(string[] args)
     {
         ) // causes CS1513 on previous line
    
     }
    

    You can verify that there is a correct number of matching braces by counting
    the number of opening braces and the number of closing braces. They should be
    the same. Preferably use a programmatic method of counting to avoid human error.

    Fix the code as suggested and the error message about expecting a } will go away.

    - Wayne

    Thursday, July 30, 2020 2:30 PM
  • i didnt work
    Saturday, August 1, 2020 5:03 AM
  • i did

    Saturday, August 1, 2020 5:07 AM
  • it didnt work
    Saturday, August 1, 2020 5:08 AM
  • i dont under stand can u just copy the wrong line and make it correct then reply
    Saturday, August 1, 2020 5:10 AM
  • it didnt work

    What does that mean *exactly*?

    Are you getting an error from the compiler? If so what is it and what line is
    causing it in your code?

    Post the code you are now using, but don't post it as an image.
    Use the forum editor feature: "Insert Code Block"

    It should be the second button/image from the right on the forum editor's
    menu/button bar.

    - Wayne

    Saturday, August 1, 2020 5:12 AM
  • i dont under stand can u just copy the wrong line and make it correct then reply

    I already did that in my very first reply.

    - Wayne

    Saturday, August 1, 2020 5:13 AM
  • Here is something to try

    Task 1

    Make sure there are brackets for things like if statements. For instance (not saying these are the problem), these should have opening and closing brackets in this case, if there were no issues with brackets perhaps they could be fine without brackets but brackets add clarity.

    Task 2

    • Go to menu Tools -> Options -> Environment -> Keyboard
    • Search for GotoBrace in the text box on the Keyboard page.
    • Assign the desired shortcut (mine was empty, so I put in CTRL + ]
    • Click the "Assign Button"

    Now place your cursor on a bracket.

    Press CTRL + ] and if there is a matching } it will take you there, if not we found an issue.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Proposed as answer by BonnieBMVP Saturday, August 1, 2020 11:57 PM
    Saturday, August 1, 2020 11:07 AM