- Home /
Help needed for begginer. With scripting items.
Hello guys so i wanted you to tell me how do i make this script shorter or universal.
first script what happens on pickup
laikyk = this.gameObject.name;
if (laikyk == "iltis") {
info.gameObject.SendMessage("ilkiekis", 1);
}
else if (laikyk == "oda") {
info.gameObject.SendMessage("odkiekis", 1);
}
second script which stores information. function ilkiekis (iltkiek : int) { tiltkiek = tiltkiek + iltkiek;
Debug.Log(tiltkiek);
}
function odkiekis (odokiekis : int) {
todkiek = todkiek + odokiekis;
Debug.Log(todkiek);
}
bassicly first script is attached to every item in the game it Works pretty well well ofc thats not entire script so i want it to be able to somehow check the name of an item and create variable with its name and each time it is called it adds 1 to it ? so i dont need to write endless else if script. My main task is to make this script fully functional with every item without creating additional script (just in case you dont know when game launches it creates seperate instance of script for each item it is attached to)
Even if your not sure any help is great.
P.S
tried using arrays no luck i mean that might help to easier give meanings but i still need to write else if scripts
Answer by Azrapse · Mar 17, 2017 at 11:01 AM
Hi. Your variable names are in... Latvian? Lithuanian? Polish? It certainly makes it harder for other people here to understand the point of your code.
I think I understand you want to have some kind of "accountant" object (named info) that keeps track of how many different items have been picked up or something.
I would do this:
public class ItemTracker
{
Dictionary<string,int> itemCountDict = new Dictionary<string,int>();
public static void RegisterItem(string itemName, int itemAmount)
{
if(itemCountDict.ContainsKey(itemName))
{
itemCountDict[itemName] += itemAmount;
}
else
{
itemCountDict.Add(itemName, itemAmount);
}
Debug.Log(itemName+": "+itemCountDict[itemName]);
}
}
Then, you would use it from somewhere else like this:
var itemName = this.gameObject.name;
ItemTracker.RegisterItem(itemName, 1);
A dictionary is a collection of elements where every element has a key and a value. When you add elements to the dictionary, you provide the key and the value. Then, to retrieve the element from the dictionary at a later point, you use the key as an index. For this example, I have created a Dictionary< string, int >, that is a dictionary where keys are of type string (for the item names) and values are of type int (for the amounts of every item).
I would very much recommend you to find a comprehensive C# course online and read it from the beginning to the end. It will teach you many of these tools that will make your programming life much much easier.
Thank you a lot this might make my life way easier yeah you understood it correctly i want to register each diffirent item without making infinite else if code which would be really unproductive and dumb. Sorry i will try to give comments what my variables mean in english. Thank you a lot again.
btw iam using javascript not c# ant this is in Lithuanian language
Your answer
Follow this Question
Related Questions
Are C# Global Variables Safe In Unity? 5 Answers
pick up item script issue 3 Answers
Pick up, drop and place tagged objects 0 Answers
I'm unable to clear a variable that is used in multiple scripts. 0 Answers