how can i shorten this code? [Solved]

i am new to scripting i know that there is a better way to write this here is the script :

public GameObject GameobjectName;
public string GameobjectTag;

public GameObject Item1;
public GameObject Item2;
public GameObject Item3;

public int WoodenPlanks = 0;
public int Log = 0;
public int HalfLog = 0;

void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
if (GameobjectTag == “WoodenPlank”)
{
GameobjectName.gameObject.SetActive(false);
WoodenPlanks = WoodenPlanks + 1;
}
}
if (Input.GetKeyDown(KeyCode.E))
{
if (GameobjectTag == “Log”)
{
GameobjectName.gameObject.SetActive(false);
Log = Log + 1;
}
}
if (Input.GetKeyDown(KeyCode.E))
{
if (GameobjectTag == “HalfLog”)
{
GameobjectName.gameObject.SetActive(false);
HalfLog = HalfLog + 1;
}
}

// that’s the code is there a smarter way to put the code? instead of having to do if statement for each GameobjectTag?
thank you!

you might also want to consider C# Coding Standards and Best Practices

you can compact things into a single “if input e” and I’d probably go with a switch statement

if (Input.GetKeyDown(KeyCode.E))
{
    switch (GameobjectTag )
    {
        case "myTag1":
                // stuff
                break;
        case "myTag2":
                // other stuff
                break;
        default:
                // stuff if it's totally the wrong tag!
                break;
    }
}
1 Like

Thank you so much LeftyRighty ! i needed the switch statement in my life! :slight_smile:

You may also consider putting the ItemX into an array, instead of individual variables

public GameObject Item1;
public GameObject Item2;
public GameObject Item3;

becomes

public GameObject Items[] = new GameObjects[3]

If you even need it at all. I don’t see the ItemX’s getting referenced anywhere

1 Like

i am going to use them soon thanks for the tip Vedrit

I would use a collection. A dictionary seems well suited to the problem

public GameObject theGameObject;
public string theTag;

Dictionary <string, int> items = new Dictionary <string, int>();

void Update() {
    if (Input.GetKeyDown(KeyCode.E)) {
          theGameObject.SetActive(false);
          if (items.ContainsKey(theTag){
              items[theTag] = items[theTag] + 1;
          } else {
              items.Add(theTag, 1);
          }
    }
}

That said your way of handling the GameObject and string at the top of your script is pretty odd. Its likely to be prone to bugs down the track. I would suggest implementing raycasting inside of your if statement to figure out what the GameObject should be.

2 Likes

i do have raycast that’s how i get the gameobject and gameobjecttag
thanks for the info i will check out the “dictionary” and see how it works out!

Yes, I agree, a dictionary is the way to go. I would also add, you should put this into a function with a good name that describes what you are doing.

Example:

     public const UnityEngine.KeyCode KEYBIND_GET_ITEM = KeyCode.E;
     Dictionary<string, int> Inventory = new Dictionary<string, int> {
       { "Wooden Planks", 0 },
       { "Log", 0 },
       { "Half Log", 0 }
     };

     void Update()
     {
       if (Input.GetKeyDown(KEYBIND_GET_ITEM))
       {
         AddToInventory(gameObject);
       }
     }

     void AddToInventory(GameObject go)
     {
       if (Inventory.ContainsKey(go.tag))
       {
         // go is a gameObject that has a tag of 'Wooden Planks', 'Log', or 'Half Log'.
         Inventory[go.tag] += 1;
         go.SetActive(false);
       }
     }

There is now a new function, ‘AddToInventory’, whose name more clearly conveys what is happening.

In your original example, you have a variable called ‘WoodenPlanks’, and an object tag ‘WoodenPlank’. This spelling difference will bite you in the future. You can avoid providing redundant information, by setting it up like above. This provides the pleasing benefit of being able to use spaces in the item name now, and automatic tag checking, which prevent non-inventory tags from being processed.

2 Likes

thank you sooo much i am still new to all of this that’s why i don’t have a good naming system
i can honestly say that i love unity and c# !!