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

Script improvement?

Discussion in 'Scripting' started by Jorjor210, Jun 10, 2015.

  1. Jorjor210

    Jorjor210

    Joined:
    May 3, 2015
    Posts:
    91
    I'm a beginner coder and I'm never 100% confident in my code I make myself even if it works flawlessly like this. Is there anything I could do to this to make it run smoother in any way? Or have I done a good job? I really enjoy coding and love to learn little tips along the way. and I found the best way I learn is finding better ways to write my scripts. :) thanks in advance!
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class growingFLoor : MonoBehaviour {
    5.     public GameObject Floor;
    6.     public GameObject prefab;
    7.     public int z= 0;
    8.     // Use this for initialization
    9.     void Start () {
    10.         z = 0;
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update () {
    15.  
    16.         if (Input.GetKeyDown(KeyCode.A)) {
    17.             z=z+3;
    18.  
    19.         Vector3 StartBlock =    Floor.transform.position;
    20.             Vector3 Spacing = StartBlock + new Vector3 (z,0,0);
    21.             Instantiate (prefab, Spacing, Quaternion.identity);
    22.         }
    23.     }
    24. }
    basically this sript is supposed to spawn a object +3 in x direction everytime the player hits A
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Most of the problems I see with this are formatting-related. "growingFLoor" should be GrowingFloor, for example. Also, traditionally class names are UpperCase while variable names are what's called camelCase; so Floor should be floor, etc.

    Functionally, the only problem I see is the choice of int for your spacing and hard-coding the difference of "3" into the code. That would be a perfect place to use float instead (it'd give you more precise control over the spacing of this stuff), and adding a variable instead of a hard-coded "3" would let you control it in the editor. In fact, I'd probably make it a Vector3 - this would allow you to let the "user" (or future developer, e.g. you in 3 weeks) to set different offsets in different directions.
     
    Jorjor210 likes this.
  3. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
    If z defines an offset on the x axis, I'd rename it to xOffset or just x if you prefer. It's also not necessary to initialize it's value twice because the default value for an int is already 0. Also, makig your public variables private and marking them with a SerializeField attribute, you can still set them via the inspector but you can remove public access to them at runtime. I'd rewrite it like this :

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class growingFLoor : MonoBehaviour {
    5.     [SerializeField]
    6.     private GameObject Floor;
    7.     [SerializeField]
    8.     private GameObject prefab;
    9.  
    10.     private int xOffset;
    11.  
    12.     void Update () {
    13.  
    14.         if (Input.GetKeyDown(KeyCode.A)) {
    15.             xOffset+=3;
    16.             Instantiate (prefab, Floor.transform.position + xOffset * Vector3.right, Quaternion.identity);
    17.         }
    18.     }
    19. }
     
    Jorjor210 likes this.
  4. Jorjor210

    Jorjor210

    Joined:
    May 3, 2015
    Posts:
    91
    Thank you both :) I'm glad to see I've at least learned enough for no major errors that are game breaking (hopefully)! but I'm also glad to hear the constructive criticism it helps a lot! I Just started coding for the first time about a month ago, and i'm starting to feel like I'm getting to the point where I know enough to be more independent , but not totally because obviously I got a lot of more advanced things to learn in the future!