0
\$\begingroup\$

I created an empty game object named "inventory", whenever I click on an object it transfers into that game object : enter image description here

and I got this far with the script:

//Transforms
public Transform Knife; //will use the knife prefab
public Transform IceBlock; //will use the iceblock prefab
public Transform Bomb;//will use the bomb prefab
public Transform Chain;//will use the chain prefab
public Transform inventory;//will use the inventory gameobject
private bool knife;
private bool ice;
private bool bomb;
private bool chain;
void fInventory()
{
    if (inventory != null)
    {
        foreach (Transform child in inventory.transform) // child is a gameobject inside of the inventory gameobject
        {
            if (child.name == Knife.name)
            {
                knife = true;
            }
            else if (child.name == IceBlock.name) { ice = true; }
            else if (child.name == Bomb.name) { bomb = true; }
            else if (child.name == Chain.name) { chain = true; }
        }
    }
}
void Update() {
    fInventory();
    if (knife)
        //use knife
    if (ice)
       //use ice
    //........etc
}
}

but as you can see, it's pretty hardcoded. So what I want this to do is whenever I pick up an gameobject with the name example:"knife", I will be able to use the Knife Transform, and while this script works it really looks ugly and inefficient. Is there any other way to check if two tranform names are the same without using an if-else statement? Hope the question was clear enough, and please just point me in a direction!

\$\endgroup\$
1
\$\begingroup\$

You can make a class of type InventoryObject. You can then create a Knife class that inherits from InventoryObject. Your inventory script can then call

InventoryObject newObjectComponent = newObject.GetComponent<InventoryObject>();

when you add a new object. Then you can test:

if(newObjectComponent is Knife)
  knife = true
else if(newObjectComponent is IceBlock)
  ...

Also, your foreach doesn't look right. I think what you want is something more like this:

foreach (Transform child in inventory.GetComponentsInChildren<Transform>())
\$\endgroup\$
  • \$\begingroup\$ Will give it a try! \$\endgroup\$ – Lendrit Ibrahimi Jun 12 '16 at 12:40
0
\$\begingroup\$

you can use Switch statement.in switch statement you can compare experision with your object name then if it was match Do something

enter image description here

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {
    public GameObject Inventory;

    // Update is called once per frame
    void Update () {
        for(int i =0;i<Inventory.transform.childCount;i++){
            switch(Inventory.transform.GetChild(i).name){
            case "Knife":
                print ("I found Knife // You can do something");break;
            case "Bomb":
                print ("I found Bomb// You can do something");break;
            case "chain":
                print ("I found chain// You can do something");break;
            }
        }
    }
}

instead of define public Transform Knife you can load them as prefab in array to shorten your script you can use Array for load prefabs

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {
    public GameObject Inventory;
    public Object[] prefabs;

public GameObject knife,chain,bomb;

    // Use this for initialization
    void Start () {
        prefabs = Resources.LoadAll("Prefabs", typeof(Object));
        for(int i =0;i<Inventory.transform.childCount;i++){
            switch(Inventory.transform.GetChild(i).name){
            case knife.name:
                print ("I found knife // You can do something");break;
            case chain.name:
                print ("I found Bomb // You can do something");break;
            case bomb.name:
                print ("I found chain // You can do something");break;
            }
        }
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.