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

Help Please - C# and Inspector problems~

Discussion in 'Scripting' started by Kayscomic, Jun 16, 2014.

  1. Kayscomic

    Kayscomic

    Joined:
    Jun 15, 2014
    Posts:
    4
    So, my script should be loading up changes in the inspector, and most the most part it does, however it refuses to show my 'inventory' updates;

    My code says there is an issue with this code;
    Code (csharp):
    1.  
    2. void Start () {
    3.         for (int i = 0; i < (slotsX * slotsY); i++)
    4.         {
    5.             slots.Add(new Item());
    6.             inventory.Add (new Item());
    7.         }
    8.  
    9.         database = GameObject.FindGameObjectWithTag("ItemDatabase").GetComponent<ItemDatabase>();
    10.         inventory [0] = database.items [0];
    11.     }
    12.  
    http://codeviewer.org/view/code:40e9 Items.cs

    http://codeviewer.org/view/code:40ea Inventory.cs

    http://codeviewer.org/view/code:40e2 itemdatabase.cs

    Any ideas, i have been working on this error for days! Any help would be gratefully appreciated!
     
    Last edited: Jun 16, 2014
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Item appears to be a non-value class. If it were int's or floats or game objects, then it would probably display. As it looks you are denoting things like this:
    Code (csharp):
    1.  
    2. List<Item> slots = new List<Item>();
    3. List<Item> inventory = new List<Item>();
    4.  
    You are not showing any code concerning what an Item is though. That class is probably your answer.
     
    Magiichan likes this.
  3. Kayscomic

    Kayscomic

    Joined:
    Jun 15, 2014
    Posts:
    4
  4. Kayscomic

    Kayscomic

    Joined:
    Jun 15, 2014
    Posts:
    4
    Help is still needed.
     
  5. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    problem 1)

    Both the item database and inventory use the Start method. There isn't much way to control which comes first.

    problem 2)

    Most of the stuff you have, requires pre-defined variables and stuff. When posting on the forums, it is not a good thing to require people to set variables and such. This makes helping you that much harder...

    Code (csharp):
    1.  
    2. private int slotsX = 4, slotsY = 4;
    3.  
    4.  
    5. // way down in the code you specify a gui style.
    6.                     if(skin != null){
    7.                         GUI.Box(slotRect, "", skin.GetStyle("Slot"));
    8.                     }else{
    9.                         GUI.Box(slotRect, "");
    10.                     }
    11.  
    (I know this seems petty, but you have to remember people are taking time out of their day to help you. ;) )

    OK, now, I set everythign up as you had it. Made a grid of 4x4 slots and looked in teh inspector and saw..... None (item)
    for each slot. I assume that this is your problem.

    Now, in testing this, I inserted this line after the one shown below... er.. teh one after that.. you will get it.

    Code (csharp):
    1.  
    2. inventory [0] = database.items [0];
    3. inventory [0] = null;
    4.  
    This forces inventory[0] to null, or no item.

    Which, of course forces an error down the road. (in the DrawInventory area)

    So you need to not do this...
    Code (csharp):
    1.  
    2. if (slots[i].itemName !=null)
    3.  
    since, slots[0] is null, that means that it can't have itemName.

    Simply change it to this:
    Code (csharp):
    1.  
    2. if (slots[i] !=null)
    3.  
    so now with inventory [0] = null then I find that they still all say none (item).

    So, now to the reason you "think" you have a problem.... It says "None" because it doesnt have an Object... You created all of this by forcing the creation using the keyword "new" This does not create a definable object in Unity. So because of this, it simply says.. "I dont have an object here."
     
    Kayscomic likes this.
  6. Kayscomic

    Kayscomic

    Joined:
    Jun 15, 2014
    Posts:
    4
    Let me start with thanking you for taking the time to help me sort out this problem.
    The info you gave me was very helpful, also I appreciate the tip I am new the the unity forums and to unity,
    everything helps.

    On a side note, Inventory [0] is defined in the ItemDatabase.cs and [0] is a "Doom Sword" so it should update and say Doom Sword rather then none? Or should [0] always be Null?

    For problem one, within unity I have set the Items.cs, ItemDatabase and Inventory to run at different times during the start up.
     
    Last edited: Jun 16, 2014
  7. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    As I explained in the last paragraph.. Nothing shows up in Unity's Inspector because none of this is attached to physical objects.

    The only ways to get it to say "Doom Sword" is to create the physical object and reference the Item component from that object, or change that whole thing to reference strings, and not objects.

    When creating a inventory script, I would not hold the inspector to anything. Just let it all happen. If you have to check something, do it in debug with something like this:

    Code (csharp):
    1.  
    2. Debug.Log(inventory[0].itemName);
    3.