Skip to main content
added 2 characters in body
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 345

Create two prefabs, one for items laying on the floor and one for items in the user's inventory.

Program a MonoBehaviour FloorItem which has a reference to an instance of your class Item.

Add it to the floor item prefab. Whenever you Instantiate the floor item prefab, set the appropriate Item on it. The FloorItem class then sets up the other components on that prefab (like the Renderer and spatial UI.Text) using the information from the Item (name, model, etc.).

When the player picks up a FloorItem, get the Item from it and then Destroy the floor item game object. Then Instantiate the inventory item prefab. This prefab will be an UI object. Put it into the object hierarchy of the inventory UI. Program a MonoBehaviour InventoryItem for that prefab. This component also needs a reference to an Item object. Just as the floor item, it uses that reference to get information about the item it represents and uses it to set up all the visual components on the game object.

Your class FloorItem / InventoryItem class would look something like this:

public class FloorItem: MonoBehaviour {
     public Item item;
     
     void Start() {
          GetComponent<MeshRenderer>().mesh = item.mesh;                  
          GetComponentInChildren<UI.Text>().text = item.name;
          // ... and other components...
     }
     // ...code to handle interactions during gameplay...
}

The code to instantiate them would look something like this:

 newFloorItem = Instantiate(floorItemPrefab, position, rotation);
 newFloorItem.GetComponent<FloorItem>.item = ItemData.sword;

By the way, if you have your class Item inherit from ScriptableObject, you can handle item types as assets inside Unity and edit their properties with the Unity inspector. This can be quite handy. How to use scriptable objects exactly would be beyond the scope of this question. But we have plenty of questions about this.

Create two prefabs, one for items laying on the floor and one for items in the user's inventory.

Program a MonoBehaviour FloorItem which has a reference to an instance of your class Item.

Add it to the floor item prefab. Whenever you Instantiate the floor item prefab, set the appropriate Item on it. The FloorItem class then sets up the other components on that prefab (like the Renderer and spatial UI.Text) using the information from the Item (name, model, etc.).

When the player picks up a FloorItem, get the Item from it and then Destroy the floor item game object. Then Instantiate the inventory item prefab. This prefab will be an UI object. Put it into the object hierarchy of the inventory UI. Program a MonoBehaviour InventoryItem for that prefab. This component also needs a reference to an Item object. Just as the floor item, it uses that reference to get information about the item it represents and uses it to set up all the visual components on the game object.

Your class FloorItem / InventoryItem class would look something like this:

public class FloorItem: MonoBehaviour {
     public Item item;
     
     void Start() {
          GetComponent<MeshRenderer>().mesh = item.mesh;
          // ... and other components...
     }
     // ...code to handle interactions during gameplay...
}

The code to instantiate them would look something like this:

 newFloorItem = Instantiate(floorItemPrefab, position, rotation);
 newFloorItem.GetComponent<FloorItem>.item = ItemData.sword;

By the way, if you have your class Item inherit from ScriptableObject, you can handle item types as assets inside Unity and edit their properties with the Unity inspector. This can be quite handy. How to use scriptable objects exactly would be beyond the scope of this question. But we have plenty of questions about this.

Create two prefabs, one for items laying on the floor and one for items in the user's inventory.

Program a MonoBehaviour FloorItem which has a reference to an instance of your class Item.

Add it to the floor item prefab. Whenever you Instantiate the floor item prefab, set the appropriate Item on it. The FloorItem class then sets up the other components on that prefab (like the Renderer and spatial UI.Text) using the information from the Item (name, model, etc.).

When the player picks up a FloorItem, get the Item from it and then Destroy the floor item game object. Then Instantiate the inventory item prefab. This prefab will be an UI object. Put it into the object hierarchy of the inventory UI. Program a MonoBehaviour InventoryItem for that prefab. This component also needs a reference to an Item object. Just as the floor item, it uses that reference to get information about the item it represents and uses it to set up all the visual components on the game object.

Your class FloorItem / InventoryItem class would look something like this:

public class FloorItem: MonoBehaviour {
     public Item item;
     
     void Start() {
          GetComponent<MeshRenderer>().mesh = item.mesh;                  
          GetComponentInChildren<UI.Text>().text = item.name;
          // ... and other components...
     }
     // ...code to handle interactions during gameplay...
}

The code to instantiate them would look something like this:

 newFloorItem = Instantiate(floorItemPrefab, position, rotation);
 newFloorItem.GetComponent<FloorItem>.item = ItemData.sword;

By the way, if you have your class Item inherit from ScriptableObject, you can handle item types as assets inside Unity and edit their properties with the Unity inspector. This can be quite handy. How to use scriptable objects exactly would be beyond the scope of this question. But we have plenty of questions about this.

added 2 characters in body
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 345

Create two prefabs, one for items laying on the floor and one for items in the user's inventory.

Program a MonoBehaviour FloorItem which has a reference to an instance of your class Item.

Add it to the floor item prefab. Whenever you Instantiate the floor item prefab, set the appropriate Item on it. The FloorItem class then sets up the other components on that prefab (like the Renderer and spatial UILabelUI.Text) using the information from the Item (name, model, etc.).

When the player picks up a FloorItem, get the Item from it and then Destroy the floor item game object. Then Instantiate the inventory item prefab. This prefab will be an UI object. Put it into the object hierarchy of the inventory UI. Program a MonoBehaviour InventoryItem for that prefab. This component also needs a reference to an Item object. Just as the floor item, it uses that reference to get information about the item it represents and uses it to set up all the visual components on the game object.

Your class FloorItem / InventoryItem class would look something like this:

public class FloorItem: MonoBehaviour {
     public Item item;
     
     void Start() {
          GetComponent<MeshRenderer>().mesh = item.mesh;
          // ... and other components...
     }
     // ...code to handle interactions during gameplay...
}

The code to instantiate them would look something like this:

 newFloorItem = Instantiate(floorItemPrefab, position, rotation);
 newFloorItem.GetComponent<FloorItem>.item = ItemData.sword;

By the way, if you have your class Item inherit from ScriptableObject, you can handle item types as assets inside Unity and edit their properties with the Unity inspector. This can be quite handy. How to use scriptable objects exactly would be beyond the scope of this question. But we have plenty of questions about this.

Create two prefabs, one for items laying on the floor and one for items in the user's inventory.

Program a MonoBehaviour FloorItem which has a reference to an instance of your class Item.

Add it to the floor item prefab. Whenever you Instantiate the floor item prefab, set the appropriate Item on it. The FloorItem class then sets up the other components on that prefab (like the Renderer and spatial UILabel) using the information from the Item (name, model, etc.).

When the player picks up a FloorItem, get the Item from it and then Destroy the floor item game object. Then Instantiate the inventory item prefab. This prefab will be an UI object. Put it into the object hierarchy of the inventory UI. Program a MonoBehaviour InventoryItem for that prefab. This component also needs a reference to an Item object. Just as the floor item, it uses that reference to get information about the item it represents and uses it to set up all the visual components on the game object.

Your class FloorItem / InventoryItem class would look something like this:

public class FloorItem: MonoBehaviour {
     public Item item;
     
     void Start() {
          GetComponent<MeshRenderer>().mesh = item.mesh;
          // ... and other components...
     }
     // ...code to handle interactions during gameplay...
}

The code to instantiate them would look something like this:

 newFloorItem = Instantiate(floorItemPrefab, position, rotation);
 newFloorItem.GetComponent<FloorItem>.item = ItemData.sword;

By the way, if you have your class Item inherit from ScriptableObject, you can handle item types as assets inside Unity and edit their properties with the Unity inspector. This can be quite handy. How to use scriptable objects exactly would be beyond the scope of this question. But we have plenty of questions about this.

Create two prefabs, one for items laying on the floor and one for items in the user's inventory.

Program a MonoBehaviour FloorItem which has a reference to an instance of your class Item.

Add it to the floor item prefab. Whenever you Instantiate the floor item prefab, set the appropriate Item on it. The FloorItem class then sets up the other components on that prefab (like the Renderer and spatial UI.Text) using the information from the Item (name, model, etc.).

When the player picks up a FloorItem, get the Item from it and then Destroy the floor item game object. Then Instantiate the inventory item prefab. This prefab will be an UI object. Put it into the object hierarchy of the inventory UI. Program a MonoBehaviour InventoryItem for that prefab. This component also needs a reference to an Item object. Just as the floor item, it uses that reference to get information about the item it represents and uses it to set up all the visual components on the game object.

Your class FloorItem / InventoryItem class would look something like this:

public class FloorItem: MonoBehaviour {
     public Item item;
     
     void Start() {
          GetComponent<MeshRenderer>().mesh = item.mesh;
          // ... and other components...
     }
     // ...code to handle interactions during gameplay...
}

The code to instantiate them would look something like this:

 newFloorItem = Instantiate(floorItemPrefab, position, rotation);
 newFloorItem.GetComponent<FloorItem>.item = ItemData.sword;

By the way, if you have your class Item inherit from ScriptableObject, you can handle item types as assets inside Unity and edit their properties with the Unity inspector. This can be quite handy. How to use scriptable objects exactly would be beyond the scope of this question. But we have plenty of questions about this.

added 75 characters in body
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 345

Create two prefabs, one for items laying on the floor and one for items in the user's inventory.

Program a MonoBehaviour FloorItem which has a reference to an instance of your class Item. 

Add it to the floor item prefab. Whenever you Instantiate the floor item prefab, set the appropriate Item on it. The FloorItem class then sets up the other components on that prefab (like the Renderer and spatial UILabel) using the information from the Item (name, model, etc.).

When the player picks up a FloorItem, get the Item from it and then Destroy the floor item game object. Then Instantiate the inventory item prefab. This prefab will be an UI object. Put it into the object hierarchy of the inventory UI. Program a MonoBehaviour InventoryItem for that prefab. This component also needs a reference to an Item object. Just as the floor item, it uses that reference to get information about the item it represents and uses it to set up all the visual components on the game object.

Your class FloorItem / InventoryItem class would look something like this:

public class FloorItem: MonoBehaviour {
     public Item item;
     
     void Start() {
          GetComponent<MeshRenderer>().mesh = item.mesh;
          // ... and other components...
     }
     // ...code to handle interactions during gameplay...
}

The code to instantiate them would look something like this:

 newFloorItem = Instantiate(floorItemPrefab, position, rotation);
 newFloorItem.GetComponent<FloorItem>.item = ItemData.sword;

By the way, if you have your class Item inherit from ScriptableObject, you can handle item types as assets inside Unity and edit their properties with the Unity inspector. This can be quite handy. How to use scriptable objects exactly would be beyond the scope of this question. But we have plenty of questions about this.

Create two prefabs, one for items laying on the floor and one for items in the user's inventory.

Program a MonoBehaviour FloorItem which has a reference to an instance of your class Item. Add it to the floor item prefab. Whenever you Instantiate the floor item prefab, set the appropriate Item on it. The FloorItem class then sets up the other components on that prefab (like the Renderer and spatial UILabel) using the information from the Item (name, model, etc.).

When the player picks up a FloorItem, get the Item from it and then Destroy the floor item game object. Then Instantiate the inventory item prefab. This prefab will be an UI object. Put it into the object hierarchy of the inventory UI. Program a MonoBehaviour InventoryItem for that prefab. This component also needs a reference to an Item object. Just as the floor item, it uses that reference to get information about the item it represents and uses it to set up all the visual components on the game object.

By the way, if you have your class Item inherit from ScriptableObject, you can handle item types as assets inside Unity and edit their properties with the Unity inspector. This can be quite handy. How to use scriptable objects exactly would be beyond the scope of this question. But we have plenty of questions about this.

Create two prefabs, one for items laying on the floor and one for items in the user's inventory.

Program a MonoBehaviour FloorItem which has a reference to an instance of your class Item. 

Add it to the floor item prefab. Whenever you Instantiate the floor item prefab, set the appropriate Item on it. The FloorItem class then sets up the other components on that prefab (like the Renderer and spatial UILabel) using the information from the Item (name, model, etc.).

When the player picks up a FloorItem, get the Item from it and then Destroy the floor item game object. Then Instantiate the inventory item prefab. This prefab will be an UI object. Put it into the object hierarchy of the inventory UI. Program a MonoBehaviour InventoryItem for that prefab. This component also needs a reference to an Item object. Just as the floor item, it uses that reference to get information about the item it represents and uses it to set up all the visual components on the game object.

Your class FloorItem / InventoryItem class would look something like this:

public class FloorItem: MonoBehaviour {
     public Item item;
     
     void Start() {
          GetComponent<MeshRenderer>().mesh = item.mesh;
          // ... and other components...
     }
     // ...code to handle interactions during gameplay...
}

The code to instantiate them would look something like this:

 newFloorItem = Instantiate(floorItemPrefab, position, rotation);
 newFloorItem.GetComponent<FloorItem>.item = ItemData.sword;

By the way, if you have your class Item inherit from ScriptableObject, you can handle item types as assets inside Unity and edit their properties with the Unity inspector. This can be quite handy. How to use scriptable objects exactly would be beyond the scope of this question. But we have plenty of questions about this.

added 75 characters in body
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 345
Loading
Source Link
Philipp
  • 123.2k
  • 28
  • 264
  • 345
Loading