OOP usage in Unity Game Engine

i’ve started to learn Unity and we were always using MonoBehaviour Parent and Child Classes. After that i realized that we need to use OOP with our projects. I know how can i create classes and objects in C# and i know essential functions for Unity. How can i learn combining both of them in order to create more advanced programes.

In other words, i don’t get how we can create some Game Objects which are not applied to Monobehaviour class, and instantiate them to the scene. Do i need other Scripts which are related with Monobehaviour class for that?

If you can help me providing some guideline to learn OOP in Unity, i’ld really appretiate for that

:/ I might have explained my problem a little bit bad but this is where i actually have issues.

For Example, with following code i created Item Class and Inventory Class, lets say I’m going to add Sprites to my “Item” how can i handle with that in Unity? Wıth MonoBehaviours i can attach Script to the game objects and take sprite from the editor.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Item {
public enum ItemType {
None,
Sword,
HealthPotion,
ManaPotion,
PowerUpPotion
}
public ItemType itemType;
public float amount;
}
// *******************************************************
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Inventory
{
public List<Item> ItemList;
public Inventory() {
ItemList = new List<Item>();
AddItem(new Item {itemType = Item.ItemType.Sword, amount = 1});
Debug.Log(ItemList.Count);
}
public void AddItem(Item item) {
ItemList.Add(item);
}
}
</code>
<code>using System.Collections; using System.Collections.Generic; using UnityEngine; public class Item { public enum ItemType { None, Sword, HealthPotion, ManaPotion, PowerUpPotion } public ItemType itemType; public float amount; } // ******************************************************* using System.Collections; using System.Collections.Generic; using UnityEngine; public class Inventory { public List<Item> ItemList; public Inventory() { ItemList = new List<Item>(); AddItem(new Item {itemType = Item.ItemType.Sword, amount = 1}); Debug.Log(ItemList.Count); } public void AddItem(Item item) { ItemList.Add(item); } } </code>
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Item {
    public enum ItemType {
        None,
        Sword,
        HealthPotion,
        ManaPotion,
        PowerUpPotion
    }

    public ItemType itemType;
    public float amount;
}

// *******************************************************

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Inventory 
{
    public List<Item> ItemList;

    public Inventory() {
        ItemList = new List<Item>();

        AddItem(new Item {itemType = Item.ItemType.Sword, amount = 1});
        Debug.Log(ItemList.Count);
    }

    public void AddItem(Item item) {
        ItemList.Add(item);
    }
}

New contributor

Melih Altuğ Çelik is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

For data assets that aren’t necessarily code, but needs to be able to modify within the editor or store Unity objects (like Sprite), Unity has ScriptableObjects.

You can create a class that inherits from ScriptableObject:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class Item : ScriptableObject {
public enum ItemType {
None,
Sword,
HealthPotion,
ManaPotion,
PowerUpPotion
}
public ItemType itemType;
public float amount;
public Sprite inventorySprite;
}
</code>
<code>public class Item : ScriptableObject { public enum ItemType { None, Sword, HealthPotion, ManaPotion, PowerUpPotion } public ItemType itemType; public float amount; public Sprite inventorySprite; } </code>
public class Item : ScriptableObject {
    public enum ItemType {
        None,
        Sword,
        HealthPotion,
        ManaPotion,
        PowerUpPotion
    }

    public ItemType itemType;
    public float amount;
    public Sprite inventorySprite;
}

then you can Right Click (in Assets window) > Create > ScriptableObjects > [Classname] inside your assets folder, and edit or refer to them like any other Unity object.

5

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật