"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): using UnityEngine; using System.Collections; using System.Collections.Generic; //using UnityEngine.UI; public class Inventory : MonoBehaviour { public GUITexture[] guiItemTextures; public GUITexture[] guiItemQuantities; private Dictionary<string,ItemProperties> items = new Dictionary<string, ItemProperties>(); void Start() { UpdateView(); } public bool AddItem(string itemName, Texture2D texture) { if(!items.ContainsKey(itemName)) { if(items.Count < guiItemTextures.Length) { ItemProperties ip = new ItemProperties(); ip.Texture = texture; ip.quantity = 1; items.Add(itemName, ip); UpdateView(); return true; } else { return false; } } else { items[itemName].quantity += 1; UpdateView(); return true; } } public bool RemovieItem(string itemName) { if(items.ContainsKey(itemName)) { if(items[itemName].quantity == 1) { items.Remove(itemName); } else { items[itemName].quantity -= 1; } UpdateView(); return true; } else { return false; } } void UpdateView() { int index = 0; int guiCount = guiItemTextures.Length; for(int i = 0; i <guiCount; i++){ guiItemTextures.texture = null; guiItemQuantities.text = ""; } foreach(KeyValuePair<string,ItemProperties> current in items) { guiItemTextures[index].texture = current.Value.Texture; guiItemQuantities[index].text = current.Value.quantity.ToString(); index++; } } }
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.
Yes: " Assets/Script/Inventory.cs(6,14): error CS0101: The namespace `global::' already contains a definition for `Inventory' "
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.
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.
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.
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.
Line 10 in your code is most likely supposed to be: Code (csharp): 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.
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): public bool RemoveItem(string itemName) { if (items.ContainsKey(itemName)) { if(items[itemName].quantity == 1) items.Remove(itemName); else items[itemName].quantity -= 1; UpdateView(); return true; } else return false; } Yeah No idea where the return true comes in I wrote it instead that way: Code (CSharp): public bool RemovieItem(string itemName) { if(items.ContainsKey(itemName)) { if(items[itemName].quantity == 1) { items.Remove(itemName); } else { items[itemName].quantity -= 1; } UpdateView(); return true; } else { return false; } } 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.
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.
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.