Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

OOP or component programming? How can I do this example?

Discussion in 'Scripting' started by Biztor, Jun 4, 2015.

  1. Biztor

    Biztor

    Joined:
    Nov 6, 2014
    Posts:
    11
    I have a Board prefab, character prefab and enemy prefab. I dont know what its the best practice.

    1- In the game logic instantiate the prefabs, each one with his Script, the game logic script, send and read messages from other scripts.

    2-In the script of the game logic, create a class for the character, in this class, create the functions,and the components of the character, and then create a instance of this class. when you want to send or read messages of this class, you dont need to find the game object, only need to go character.isDead() (for example).
    the question is, this is better? Im wrong with something?

    if this is better, Can I create a prefab (for example the main character), and then when I create an instance of the class, get the components of the prefab instead creating all in the code?.

    for example I have a prefab with multiple Game objects,or with spriterenderer or audio. If I need to create all in code, there is too much work.

    Im very lost in this OOP in unity. I always do the 1º thing, but I see the roguelike tutorial, and another scripting tutorials, and now I dont know what is the best practice for programming in unity.

    Could anyone help me? thanks
     
  2. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
    Prefabs are useful for setting up your components beforehand. In some cases I'd rather attach everything I need in the code rather than instantiate a prefab but having a prefab with all the scripts you need beforehand is not wrong.

    About components, you should use OOP to derive base components when there is common features between various types of components. You should separate in distinct components features that would represent a "behaviour" of a gameObject. So, you could create for example a component that handles pathfinding and movement, and use inheritance to create various types of pathfinding and movement behaviours, encapsulating common features of pathfinding and movement in a base class. But you would create a separate component for managing a character's data, for example. Your Character component would take care of managing your character's health and stuff like that and your movement component would move your character around. Your Character component could simply use GetComponent<BaseMovementComponent>() to get the component and set a destination or stuff like that but the movement itself would be handle by the movement component.

    Do I make sense?
     
    LeftyRighty and dterbeest like this.
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    totally agree with the "small specific" components mentality, also has the benefit of being highly reusable :)
     
  4. Biztor

    Biztor

    Joined:
    Nov 6, 2014
    Posts:
    11
    Im very lost...

    Ill try to do a example.

    I create a prefab, with a sound component, sprite renderer, and some other game objects with sprites attached to the parent.

    In my main script:

    public class char
    {
    float life;
    public GameObject payerprefab; // how can I set the prefab I made before here as a propierty???
    public SpriteRenderer sprite; // same.

    public char(float x)
    {
    life=x;

    }

    }


    public class Game : MonoBehaviour
    {
    GameObject test= new char(18) as GameObject // this shows an error

    }


    do you undestanding me now?
     
  5. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
    I'm not sure I'm following you. Your 'char' class, is it a MonoBehaviour? If it's a MonoBehaviour, you cannot instantiate it with the 'new' keyword, you must instead add it to a GameObject with the AddComponent<char>() method. Also, char is a reserved keyword, you should name your class Character or something like that.

    Code (CSharp):
    1. public class Game : MonoBehaviour
    2. {
    3.     void Awake()
    4.     {
    5.         GameObject test = new GameObject();
    6.         Character character = test.AddComponent<Character>();
    7.         character.life = 18;
    8.  
    9.         //etc.
    10.     }
    11. }
    12.  
     
  6. Biztor

    Biztor

    Joined:
    Nov 6, 2014
    Posts:
    11
    If I do that, I get an error
    The type `Character' must be convertible to `UnityEngine.Component' in order to use it as parameter `T' in the generic type or method `UnityEngine.GameObject.AddComponent<T>()'

    anyways I need the components of my prefab in the propierties of the class.

    I think I dont understand the pros of the OOP, I need and example, because I think im asking crazy things...

    In a breakout/arkanoid game. Can I create all the game in 1 script, creating instances of all?
    creating an instance of a ball, with his gameobject, spriter renderer ,rigidbody and behaviour, with a OntriggerEnter() method inside the class?
    creating an instance of the paddle, with all the same type of components like before and his own behaviour.

    And the comunication are betwen this instances inside this script, and no gameobject search is required. if Im correct, how can I do this?

    Thanks for your patience.
     
  7. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
    You CAN create your gameObject from one script that will "initialize" your scene. I think your issue right now is differentiating between creating C# objects and Unity's GameObjects and Components. The scripts you can attach on your prefabs are components and they must derive the MonoBehaviour class. This will allow your script to get Start() and Update() callbacks, among others.


    Code (CSharp):
    1. public class Character : MonoBehaviour
    2. {
    3.     public float life;
    4.     public GameObject payerprefab;
    5.     public SpriteRenderer sprite;
    6.  
    7.     void Start()
    8.     {
    9.         sprite = GetComponent<SpriteRenderer>();
    10.     }
    11. }
    Try using this component. The game script I posted earlier should work with this. You can also drag and drop your Character class on a game object in the scene and save it as a prefab by dragging the gameobject in your project window.

    It is imperative that you understand the difference between components and regular C# objects to make a game in Unity. I suggest you watch the tutorials to make sure you understand these concepts correctly.
     
  8. Biztor

    Biztor

    Joined:
    Nov 6, 2014
    Posts:
    11
    I did 2 scrips with your code, Only create a GameObject with a Character script as component. But not the playerprefab or sprite renderer.

    I watched the unity tutorials, but dont explain what im asking. This is very frustrated.