Search Unity

Is there a way to make a game based on script?

Discussion in 'Community Learning & Teaching' started by korrit, Jun 29, 2015.

  1. korrit

    korrit

    Joined:
    Jun 29, 2015
    Posts:
    3
    I'm relatively new to unity and wanted to to use scripting as the way to make games (I'm a programmer and have made games with other languages), but from what I could learn about scripting is that it is generally used for behaviour of game objects, is there a way that I can make a game from scripting and have the ability to call game objects from the script itself? For example could I have a function that displays a cube(example game object) or a class in the script that displays a cube without me needed to make the game object and adding script to it for functionality?
    In advance I very much appreciate whoever contributes to the thread :D .
     
  2. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    Yes that can be done, but frankly most objects will be too complicated for that to be practical. Like, having a visual editor is a big advantage for complex objects and scenes, and the level editor is a big deal in almost all game engines.

    That said, you can create primitive objects in code and then add scripts to them to control their behavior.
     
  3. korrit

    korrit

    Joined:
    Jun 29, 2015
    Posts:
    3
    So does that mean that I need to make an empty game object and write the script from there?
    when I tried it, there was nothing that got displayed on my screen, here is what I tried:

    usingUnityEngine;
    usingSystem.Collections;

    publicclassgamemainScript : MonoBehaviour {

    //Usethisforinitialization
    voidStart () {
    GameObjectcube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    cube.transform.position = newVector3(0, 0.0F, 0);
    }

    //Updateiscalledonceperframe
    voidUpdate () {

    }
    }

    does that mean that there are other functions that are needed to display and modify that primitive
     
    Last edited: Jun 29, 2015
  4. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    Yeah, you do need to have something in the scene to attach the first script to, and then all script execution starts there. But that originator object can just be an empty object called something like "Game Controller".
     
  5. korrit

    korrit

    Joined:
    Jun 29, 2015
    Posts:
    3
    can you reply with some example script, or by telling me what I am doing wrong here
    (I managed to get one cube on the screen but what if I wanted to grid the cubes like how I tried with the for loop, it just doesn't display):

    usingUnityEngine;
    usingSystem.Collections;

    publicclassgamemainScript : MonoBehaviour {

    //Usethisforinitialization
    voidStart () {
    GameObjectcube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    }

    //Updateiscalledonceperframe
    voidUpdate () {

    for (intx = 0; x < 100; x++) {
    for (intz = 0; z < 100; z++) {
    cube.transform.position = newVector3 (x, 0, z);
    }
    }

    }
    }
     
  6. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    Your variable 'cube' is local to the Start() method, not shared between both methods. What you want is to declare a 'cube' field for the entire script, like so:

    public class GameScript : MonoBehaviour {
    private GameObject cube;
    void Start() {
    cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    }
    etc.
    }

    Notice how I declare the variable outside Start() but then use the already declared variable inside. That kind of variable (one that's part of the entire script/object, not just one function/method) is called a 'field'.

    (If you need more help beyond this, I might recommend my book in my sig. Although I don't create any entire games this way, there are some examples of creating primitive objects in code.)