Search Unity

Can't add Script

Discussion in 'Getting Started' started by Ezydenias, Jan 22, 2015.

  1. Ezydenias

    Ezydenias

    Joined:
    Jan 22, 2015
    Posts:
    10
    "Can't add component 'Inventory´' because it doesn't exist. Check to see if the file name and class name match"

    Well here is the problem, they do and I have no idea how to solve it other than add Component/New Script and than the script is added without any Errors except I try to add this new script to anything else.
    The Filename and the Class Name are Identical. And I put in the code directly from the Book I am having. It is called "Spiele entwickeln mit Unity". It was with CD and I already tried to use the original script from the files of the done Project "BeispielGame" to see that it makes no difference at all. Plus the compiler of MonoDevelop tells me that "text" isn't a function but the Author of the Book seems to think is a function of any of the libraries that are importet.

    Here is the script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. //using UnityEngine.UI;
    5.  
    6. public class Inventory : MonoBehaviour
    7. {
    8.  
    9. public GUITexture[] guiItemTextures;
    10. public GUITexture[] guiItemQuantities;
    11. private Dictionary<string,ItemProperties> items = new Dictionary<string, ItemProperties>();
    12.  
    13. void Start()
    14. {
    15.     UpdateView();
    16. }
    17.  
    18. public bool AddItem(string itemName, Texture2D texture)
    19.     {
    20.         if(!items.ContainsKey(itemName))
    21.         {
    22.             if(items.Count < guiItemTextures.Length)
    23.             {
    24.                 ItemProperties ip = new ItemProperties();
    25.                 ip.Texture = texture;
    26.                 ip.quantity = 1;
    27.                 items.Add(itemName, ip);
    28.                 UpdateView();
    29.                 return true;
    30.             }
    31.             else
    32.             {
    33.                 return false;
    34.             }
    35.         }
    36.         else
    37.         {
    38.             items[itemName].quantity += 1;
    39.             UpdateView();
    40.             return true;
    41.         }
    42.     }
    43.  
    44.  
    45.  
    46.  
    47.  
    48.  
    49.  
    50. public bool RemovieItem(string itemName)
    51. {
    52.         if(items.ContainsKey(itemName))
    53.         {
    54.             if(items[itemName].quantity == 1)
    55.             {
    56.                 items.Remove(itemName);
    57.             }
    58.             else
    59.             {
    60.                 items[itemName].quantity -= 1;
    61.             }
    62.             UpdateView();
    63.             return true;
    64.         }
    65.         else
    66.         {
    67.             return false;
    68.         }
    69. }
    70.  
    71. void UpdateView() {
    72.         int index = 0;
    73.         int guiCount = guiItemTextures.Length;
    74.      
    75.         for(int i = 0; i <guiCount; i++){
    76.             guiItemTextures.texture = null;
    77.             guiItemQuantities.text = "";
    78.         }
    79.      
    80.         foreach(KeyValuePair<string,ItemProperties> current in items)
    81.         {
    82.             guiItemTextures[index].texture = current.Value.Texture;
    83.             guiItemQuantities[index].text = current.Value.quantity.ToString();
    84.             index++;
    85.         }
    86.      
    87.     }
    88.  
    89.  
    90. }
     
    Last edited: Jan 23, 2015
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    //using UnityEngine.UI; may be change to: using UnityEngine.UI;
     
  3. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
  4. Ezydenias

    Ezydenias

    Joined:
    Jan 22, 2015
    Posts:
    10
    I tried that already, I just showed it how it is supposed to be according to the book and the file.
    using UnityEngine.UI makes no difference.
     
  5. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Do you get any error in the Unity console?
     
  6. Ezydenias

    Ezydenias

    Joined:
    Jan 22, 2015
    Posts:
    10
    Yes: " Assets/Script/Inventory.cs(6,14): error CS0101: The namespace `global::' already contains a definition for `Inventory' "
     
  7. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Whenever you have a compilation error, it has to be resolved before you can proceed with anything else. E.g. it won't be possible to enter the play mode, it won't be possible to add scripts that were not compiled in the previous successful compilation.
    At the moment you have two Inventory scripts. That is not allowed, unless you move one of them to a different namespace.
     
  8. Ezydenias

    Ezydenias

    Joined:
    Jan 22, 2015
    Posts:
    10
    But since when do I have two of them? I only made one? Is there new code and since the book came out in 2014 the made something called Inventory? Well how does this work with the namespaces. Sorry I am pretty new to object oriented programming, I come from micro controller programming.
     
  9. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    The error message clearly tells you that there are two Inventory classes, so I am almost sure you added them, even if you did it accidentally. You can just search for the second one e.g. in MonoDevelop with a project wide text search.
     
  10. Ezydenias

    Ezydenias

    Joined:
    Jan 22, 2015
    Posts:
    10
    Okay found it I had the copy of the original still in the project. But now it trhows an error at me because of the .text line 77 and 83. And I still have no idea why the author wrote that if it cannot compute? Can I just leave them out I mean they are clearly there to present the quantities in text form but it doesn't seem to work that way if Unity doesn't know those definitions.

     
  11. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Line 10 in your code is most likely supposed to be:
    Code (csharp):
    1. public GUIText[] guiItemQuantities;
    Just for your information: GUIText and GUITexture are both legacy. You should instead consider to use the Unity UI instead. I certainly understand that your book doesn't cover it.
     
  12. Ezydenias

    Ezydenias

    Joined:
    Jan 22, 2015
    Posts:
    10
    You mena UnityGui? Or the new uGUI System that comes with 4.6? Well it covers this new uGUI System but partly and you know they often use old stuff in the Tutorials. At least over here in Germany they really love using overhauled ways to do things.

    And thanks it works now. Kinda should have thought about it first considering the book made some weird "if" operations, which I have no Idea if they would work because see it for your self, I corrected them the way I understand them from C:


    Code (CSharp):
    1.     public bool RemoveItem(string itemName)
    2.     {
    3.         if (items.ContainsKey(itemName))
    4.         {
    5.             if(items[itemName].quantity == 1)
    6.                 items.Remove(itemName);
    7.             else
    8.                 items[itemName].quantity -= 1;
    9.  
    10.             UpdateView();
    11.             return true;
    12.         }
    13.         else
    14.             return false;
    15.     }
    Yeah No idea where the return true comes in I wrote it instead that way:


    Code (CSharp):
    1. public bool RemovieItem(string itemName)
    2. {
    3.         if(items.ContainsKey(itemName))
    4.         {
    5.             if(items[itemName].quantity == 1)
    6.             {
    7.                 items.Remove(itemName);
    8.             }
    9.             else
    10.             {
    11.                 items[itemName].quantity -= 1;
    12.             }
    13.             UpdateView();
    14.             return true;
    15.         }
    16.         else
    17.         {
    18.             return false;
    19.         }
    20. }

    I mean is that right this way or also the other way the author wrotes? If this is also correct than is that C# Specific or object oriented. And is that better that way or just bad style to write it that way? Well I only can say I am confused but I gues the UpdateView() comes in the uper if clause.
     
  13. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Unity UI was previously called uGUI, but Unity decided to name it Unity UI instead.

    The changes you did are identical to the original one. It is just a syntactic variant. This has nothing to do with object oriented programming at all, it is just syntactic sugar.

    Edit: Forgot that link https://msdn.microsoft.com/en-us/library/5011f09h.aspx
    For an if statement, the curly braces are optional, if the block is just a single statement.
     
  14. Ezydenias

    Ezydenias

    Joined:
    Jan 22, 2015
    Posts:
    10
    Thanks. Never knew that.
     
  15. goat

    goat

    Joined:
    Aug 24, 2009
    Posts:
    5,182
    Sounds like you have imported two different public domain Inventory Unity package assets. For example nGUI has an example inventory system.

    Maybe you can ask one of the folks in this thread to port the Inventory system you're trying to use to Unity 4.6.1 and Unity UI?

    http://forum.unity3d.com/threads/scripts-useful-4-6-scripts-collection.264161/page-2

    Also, no matter which you choose, you probably would do good for yourself to encapsulate the Inventory package you are importing with a unique namespace for that asset.