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 support with scripting error...

Discussion in 'Scripting' started by SassMaster, Sep 21, 2014.

  1. SassMaster

    SassMaster

    Joined:
    Sep 16, 2014
    Posts:
    8
    Firstly, you're probably wondering why this isn't on Unity Answers. I've tried twice. I've already written out two questions related to this and as soon as I tried to post them, they mysteriously disappeared and were apparently being reviewed by moderators. That was two days ago. I can't tell if the website ran into some kind of problem or what, but the questions were perfectly normal, and I don't see why a moderator would have rejected either of them.

    I'm gonna keep this short, because I've already written this out twice.

    So, I've got a class here for an inventory, and I'm hoping to make a small API for the inventory so I can just add and remove items wherever I need to, using the AddItem(), RemoveItem() and PlaceItem() functions. I'm programming in C#, and I keep getting a problem with this code:

    error CS1501: No overload for method `RemoveItem' takes `0' arguments

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Inventory : Items {
    6.  
    7.     GameObject[] inventoryArray = new GameObject[6];
    8.  
    9.     void Update ()
    10.     {
    11.         if(Input.GetKeyDown("L"))
    12.         {
    13.             RemoveItem();
    14.         }
    15.         if (Input.GetKeyDown("K"))
    16.         {
    17.             AddItem();
    18.         }
    19.         GameObject equippedItem = inventoryArray[1];
    20.     }
    21.  
    22.     void AddItem(GameObject i)
    23.     {
    24.         if(Input.GetKeyDown(KeyCode.P)) {                        //Checks through inventory for null spaces, then adds
    25.             int j = 0;                                            //GameObject i to the the null spot, i being the item the
    26.             for (j = 1; j < inventoryArray.Length; j++)         //player wants to add. Break terminates the function once the
    27.             {                                                    //item is added to prevent adding more than one item to the
    28.                 if (inventoryArray [j] = null)                    //inventory's null spaces.
    29.                 {
    30.                     inventoryArray [j] = i;
    31.                     break;
    32.                 }
    33.             }                                                  
    34.         }
    35.     }
    36.     void RemoveItem(GameObject i) //Used for dropping NOT placing, should be the only possible method of removing an item
    37.     {                                //other than PlaceItem();
    38.         if(Input.GetKeyDown(KeyCode.Q))
    39.         {
    40.             int j = 0;
    41.             for (j = 1; j < inventoryArray.Length; j++)
    42.             {
    43.                 if(inventoryArray[j] = i)
    44.                 {
    45.                     inventoryArray[j] = null;
    46.                 }
    47.             }
    48.  
    49.         }
    50.     }
    51. }
    52.  
    53.  
    There you go, now hopefully someone can finally answer this.
     
    Last edited: Sep 21, 2014
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
  3. Deleted User

    Deleted User

    Guest

    Here's your error:

    Code (CSharp):
    1. if(Input.GetKeyDown("L"))
    2. {
    3. RemoveItem();
    4. }
    You're calling RemoveItem but not passing a GameObject to it when your RemoveItem function requires a GameObject:

    Code (CSharp):
    1. void RemoveItem(GameObject i)
    2. { //other than PlaceItem();
    3. if(Input.GetKeyDown(KeyCode.Q))
    4. {
    5. int j = 0;
    6. for (j = 1; j < inventoryArray.Length; j++)
    7. {
    8. if(inventoryArray[j] = i)
    9. {
    10. inventoryArray[j] = null;
    11. }
    12. }
    Actually, I see that it's the same with AddItem(). After you fix RemoveItem, AddItem will give you an error as well.
     
  4. SassMaster

    SassMaster

    Joined:
    Sep 16, 2014
    Posts:
    8
    I'm sure that if Unity Answers would have let me ask a question, you'd know this already but it seems that I forgot to tell you that I want to be able to pass a "GameObject i" into the function at run-time, rather than have this be hardcoded.

    The item that the player wants to remove should be passed into the function as GameObject i, instead of having one hardcoded GameObject there. Otherwise, there would be no need for the class.
     
  5. Deleted User

    Deleted User

    Guest

    You can do that but you still need to pass a GameObject to the function when it's called because right now, nothing is passed to it.

    What GameObject do you want to pass to it at run-time?

    Try adding a public GameObject variable and set the GameObject you want to pass to it in the inspector (after changing the function to take a GameObject).
     
  6. SassMaster

    SassMaster

    Joined:
    Sep 16, 2014
    Posts:
    8
    The GameObject I want to pass is the object that the player chooses to drop from his inventory. I assumed I could make it so when the player right clicks on said item I could pass that item through the RemoveItem() function and remove it.

    Either way, I assume you mean that I should pass a kind of placeholder GameObject so that the argument is satisfied, but I'm not actually doing any dropping, and then just change said variable at runtime using other scripts whenever I want to drop something?
     
  7. SassMaster

    SassMaster

    Joined:
    Sep 16, 2014
    Posts:
    8
    Worked like a charm, except we all know that charms don't actually work, but putting superstition and stuff aside, it works quite well. I have yet to swap the placeholder object to another GameObject that will be dropped from the inventory.