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. Dismiss Notice

Separating a large script into multiple smaller scripts

Discussion in 'Scripting' started by Gatoray, Jul 2, 2014.

  1. Gatoray

    Gatoray

    Joined:
    Jun 17, 2014
    Posts:
    7
    I am attempting to make my first real game. I understand the basics of how Unity works since I've been working with it on and off for several months now and I've made a few very simple working prototypes to learn the ropes.

    However, I am slowly realizing that C# is almost nothing like C++, the language where most of my coding experience lies. I have all but succeeded in attempts to separate a single script that works exactly the way I want it to and organize it better by separating into different scripts; such as one for initialization of game objects and another for controlling the movement of those said objects. etc.

    I have watched a few of Unity's tutorials on advanced scripting such as inheritance and polymorphism, but my problem doesn't seem to be addressed as clearly as I'd like since I understand how these things work, just not really in the context of C#. So I was wondering if any of you could lead me in the right direction on how to separate my big file, link scripts together and maintain an organized set of scripts.

    I'm used to "#include" with C++, but I'm not sure what the C# equivalent of that is. Take a look at my code below and see if you have any advice for me. I apologize for my lack of comments. Thanks in advance.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnemyController2 : MonoBehaviour {
    5.  
    6.     public int rowNum; //Number of rows
    7.     public int numPerRow; //Number of enemies per row
    8.     public float bound; //Space between boundaries
    9.     public float trackSpacing; //Vertical space between rows
    10.     public GameObject enemy; //Load enemy prefab here
    11.     public float spinRatio;
    12.     public float speed;
    13.    
    14.     GameObject[][] eCont;
    15.    
    16.     float angle = 0;
    17.    
    18.     // Use this for initialization
    19.     void Start () {
    20.        
    21.         rowNum--;
    22.    
    23.         eCont = new GameObject[rowNum+1][]; //initialize enemy array with number of rows
    24.        
    25.         for (int i=0; i<=rowNum; i++)
    26.         {
    27.             eCont[i] = new GameObject[numPerRow]; //Add an array to hold the enemies
    28.             for (int j=0; j<numPerRow; j++)
    29.             {
    30.                 //Make game object and add to the array
    31.                 eCont[i][j] = (GameObject) Instantiate(enemy, new Vector3 (
    32.                 (((float)j/(float)numPerRow)*(bound*2) - bound) + transform.position.x,
    33.                 (i * trackSpacing) + transform.position.y,
    34.                 0 + transform.position.z
    35.                 ),Quaternion.identity);
    36.             }
    37.         }
    38.        
    39.        
    40.         GameObject edge1 = GameObject.CreatePrimitive(PrimitiveType.Cube);
    41.         GameObject edge2 = GameObject.CreatePrimitive(PrimitiveType.Cube);
    42.         edge1.transform.position = new Vector3(bound, 0, 0);
    43.         edge2.transform.position = new Vector3(-bound, 0, 0);
    44.     }
    45.    
    46.     // Update is called once per frame
    47.     void FixedUpdate () {
    48.        
    49.         for (int i=0; i<=rowNum; i++)
    50.         {
    51.             for (int j=0; j<numPerRow; j++)
    52.             {
    53.                
    54.                 eCont[i][j].transform.Translate(new Vector3(((spinRatio*i)+1) * speed/100, 0, 0));  
    55.             }
    56.         }
    57.     }
    58.    
    59.     void Update () {
    60.        
    61.         for (int i=0; i<=rowNum; i++)
    62.         {
    63.             for (int j=0; j<numPerRow; j++)
    64.             {  
    65.                
    66.                 if (eCont[i][j].transform.position.x > bound)
    67.                     eCont[i][j].transform.position = new Vector2(-bound, eCont[i][j].transform.position.y);
    68.                
    69.                 if (eCont[i][j].transform.position.x < -bound)
    70.                     eCont[i][j].transform.position = new Vector2(bound, eCont[i][j].transform.position.y);
    71.                
    72.             }
    73.         }
    74.        
    75.     }
    76. }
     
  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    #include is actually "using" at the top of your file.

    Code (csharp):
    1. using System;
    This mean this class can access the "System" namespace.

    Frankly, C# is a copy of C++, only without all the pain in the ass like the .h files. Compiling C# is also blazing fast!

    If you want a class to be written on multiple files, you can use the "partial" keyword, which give the compiler the instruction to link multiple files.

    Also, C#/.NET has a HUGE documentation about just everything. Microsoft actually got very impressive about that.
     
  3. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    You don't need to include anything in terms of linking. Everything is included by default. In terms of macros it's a bad idea anyway.
     
  4. Gatoray

    Gatoray

    Joined:
    Jun 17, 2014
    Posts:
    7
    Thanks for your replies.

    But let's say there is a ray in another script that controls a crosshair on the screen. Now, how should I set up my scripts to allow that same ray that is in the script attached to the crosshair object to be used in my enemy script to detect if the ray hit my enemy?

    I'm just confused on how to get all my scripts to interact with each other.
     
  5. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Expose field to the inspector and link object in it.
    GameObject.Find, GameObject.FindWithTag, GameObject.FindGameObjectsWithTag,
    Singleton pattern
    Static variable
    Physic (OnTrigger, OnCollide, Physic.Raycast, Physic.SphereOverlap, etc.)
    Custom event system
    And who know how many other way there is for two object to find and communicate with each other.
     
  6. Gatoray

    Gatoray

    Joined:
    Jun 17, 2014
    Posts:
    7
    Okay, so there's really no "best" way to do it, it's just preference?
     
  7. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    It's a case by case basis...