A
A
Artem Biryukov2015-06-08 12:34:48
C++ / C#
Artem Biryukov, 2015-06-08 12:34:48

How to write a game using only scripts in Unity?

We started with a friend to make a game on Unity, only on scripts, that is, the minimum number of objects on the scene, only the camera and one object, which, as it were, launches the Main script, which should run everything else that is needed, and so on.
In general, the bottom line is this, I'm a little confused about how to write a game in general. To give an example, I create a script without MonoBehaviour like this:

using UnityEditor;
using UnityEngine;

namespace Assets.Scripts
{
    public class Animal
    {
        public GameObject go = new GameObject("", typeof(CircleCollider2D), typeof(Rigidbody2D), typeof(SpriteRenderer));

        public Animal()
        {
            go.name = "Animal";
            go.GetComponent<Rigidbody2D>().gravityScale = 0f;
            go.transform.position = new Vector3(0, 0);
            go.transform.rotation = new Quaternion(0, 0, 0, 0);
            Debug.Log(go.name);
        }

    public class Cat : Animal
    {
        public GameObject go = new GameObject("", typeof(CircleCollider2D), typeof(Rigidbody2D), typeof(SpriteRenderer));

        public Cat()
        {
            go.name = "Cat";
            go.GetComponent<Rigidbody2D>().gravityScale = 0f;
            go.transform.position = new Vector3(0, 0);
            go.transform.rotation = new Quaternion(0, 0, 0, 0);
            Debug.Log(go.name);
        }
    }
}

Is it worth doing this at all, because it seems to me that something is wrong, although it may be right. I just judge based on the fact that if I make a class inherited from MonoBehaviour, then then you can’t just create an object instance through new (well, it’s understandable why). It doesn’t seem to suit me that I have to create an object in advance, attach a script to it, and then it will work when I can simply create an object from the constructor that will have all the functions I need, pre-written. If anyone understood me, please explain what to do with this, how I can bring all this to its logical conclusion, what for. For example, I still have a script with control, it already comes from MonoBehaviour, I hook it, for example, through AddComponent to the cat and control it, but is this correct from the point of view of designing the whole game?
In fact, it is very convenient to call everything from runtime, there are some problems with understanding all this, if someone can tell you what we may encounter during development.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
G
GavriKos, 2015-06-08
@impowski

1) Learn what prefabs are
2) You can do this. As well as hammering nails with a microscope.

E
Espleth, 2015-06-09
@Espleth

And also read how inheritance is arranged, you clearly don’t understand this moment

V
Vladislav Pedenko, 2015-07-15
@LittleGod

There is such a plug-in for Unity3d - PlayMaker - www.hutonggames.com
It allows you to make games in Unity3d without direct programming by the so-called visual programming. It's similar to Blueprint in Unreal Engine.
But to be honest, I would still advise you not to do this, but to take it and start learning C# or javascript normally.

G
Georgy Pelageikin, 2015-08-09
@ArXen42

Perhaps the description of my game will help you - I'm also a beginner, but I've been doing it for more than six months, the general architecture has more or less settled down. The essence of the game is inspired by the Space Rangers - the universe, consisting of stars, the main place of action is star systems, the game takes place inside them. Accordingly, before the start of the game, there are practically no objects - only dummies and scripts for them, and of course the UI. When you start the game from a save (or randomly generated) the universe is an array of stars, which in turn contain planets, belts, etc. This data is not related to MonoBehaviour in any way, just a static array (List actually, but not the point). At the start of the game, again, the location of the player, information about his ship is taken from the save, and in accordance with this
1) The current system spawns: the sun, planets (there is a planet prefab, when spawning, a script hanging on it imposes a randomly generated texture, and another script places it in space in accordance with the specified orbit parameters), etc.
2) The player spawns (and other players in the future)
Thus, the scene contains only the player's dummy with scripts hung on it and UI objects. Everything else is created as the game progresses.

S
Sergey Netyagin, 2015-11-05
@SN_007

Of course, you can write in the way you suggest: create everything in code, and manage only one object. Probably, for some cases this will be a reasonable solution (I don’t know what your task is). For your case, you can create objects in advance, save them in prefabs (it's very simple), and then create the required number of objects from the code and do whatever you want with them. In any case, an object created from a prefab (for example, using the Instantiate method) will have a minimal set of mechanisms for manipulating it. At a minimum, any object has a Transform component. Also, various other components can be attached to any object from the code in the process (and also unhooked if necessary). All this is possible. The main thing is to understand whether such an approach is really justified. If you think,

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question