Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by justasxz · Mar 16, 2017 at 08:29 PM · scripting problemvariablespickupstoring

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image justasxz · Mar 17, 2017 at 11:17 AM 0
Share

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.

avatar image justasxz · Mar 17, 2017 at 02:04 PM 0
Share

btw iam using javascript not c# ant this is in Lithuanian language

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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

Statics variables in structs for jobs system 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges