• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Davidblackberry1 · Apr 15, 2018 at 03:52 PM · scripting problemconfusedbig

Script becoming too big to maintain?

So I've got a script on the player that handles basically everything that has to do with the player, but I'm not really sure what to do when the script gets this big, it's getting too big to handle and I'm not sure what to do if it were to grow to like 1,000 lines at some point. Should I be breaking this up even though it all seems to have something to do with the player?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityStandardAssets.Characters.FirstPerson;
 using UnityEngine.EventSystems;
 
 public class PlayerScript : DavidBehaviour{
 
     public List<RaycastResult> Results = new List<RaycastResult>();
 
     public string[] Recipes;
     public GameObject[] Objects;
 
     public GameObject SelectedItem;
     public GameObject TargetedBlock;
     public GameObject SelectionBar;
     public GameObject TheCamera;
 
     public GameObject CraftingGrid;
     public GameObject Inventory;
     public GameObject OutputSlot;
     public GameObject Menu;
 
     public GameObject MouseSlot;
 
     public GameObject[] HotSlots;
     public GameObject[] InventorySlots;
     public GameObject[] AllSlots;
 
     public GameObject[] CraftingGridSlots;
 
     public float MoveSpeed;
     public float WalkProgress;
 
     public int SelectedSlotNumber;
 
     public bool InventoryFull;
     public bool Grounded;
     public bool MenuShown;
 
     public Texture2D MouseTexture;
 
     void Start () {
         //Cursor.SetCursor (MouseTexture, Vector3.zero, CursorMode.Auto);
         Cursor.lockState = CursorLockMode.Locked;
     }
 
     void Update ()
     {
         if (MouseSlot.GetComponent<SlotScript> ().ContainedItem != null) {
             MouseSlot.SetActive (true);
             MouseSlot.transform.position = Input.mousePosition;
         } else {
             MouseSlot.SetActive (false);
         }
 
         if (AllSlots [SelectedSlotNumber]) {
             SelectedItem = (AllSlots [SelectedSlotNumber]);
         } else {
             SelectedItem = null;
         }
 
         if (Input.GetKeyDown ("e")) {
             if (MenuShown) {
                 MenuShown = false;
                 Menu.SetActive (false);
                 Cursor.visible = false;
                 Cursor.lockState = CursorLockMode.Locked;
             } else {
                 MenuShown = true;
                 Menu.SetActive (true);
                 Cursor.visible = true;
                 Cursor.lockState = CursorLockMode.None;
             }
         }
 
         if (MenuShown == true) {
             GetComponent<CameraMouseLook> ().CanMove = false;
         } else {
             GetComponent<CameraMouseLook> ().CanMove = true;
         }
 
         if (!MenuShown) {
             if (Input.GetMouseButtonDown (1)) {
                 if (SelectedItem != null){
                     if (SelectedItem.GetComponent<SlotScript>().ContainedItem.GetComponent<ItemScript>().BlockForm != null){
                         var Block = SelectedItem.GetComponent<SlotScript>().ContainedItem.GetComponent<ItemScript> ().BlockForm;
                         Ray TheRay = new Ray (Camera.main.transform.position, Camera.main.transform.forward);
                         RaycastHit HitResult;
                         if (Physics.Raycast (TheRay, out HitResult, 5)) {
                             if (Vector3.Distance (HitResult.transform.gameObject.transform.position + HitResult.normal, transform.position) >= 1) {
                                 var PlacedBlock = Instantiate (Block, HitResult.transform.gameObject.transform.position + HitResult.normal, Quaternion.identity);
                                 PlacedBlock.name = SelectedItem.name;
                                 PlaySound ("PlaceBlock", PlacedBlock.transform.position, (3 + Random.value) / 3.5f, 2);
                                 ChangeItemAmount (SelectedItem.GetComponent<SlotScript>().ContainedItem, -1);
                             }
                         }
                     }
                 }
             }
 
             if (Input.GetKeyDown ("q")) {
                 if (HotSlots [SelectedSlotNumber].GetComponent<SlotScript>().ContainedItem != null) {
                     var DroppedItem = Instantiate (HotSlots [SelectedSlotNumber].GetComponent<SlotScript>().ContainedItem, Camera.main.transform.position + Camera.main.transform.forward, Quaternion.identity);
                     DroppedItem.GetComponent<ItemScript> ().ItemAmount = 1;
                     DroppedItem.SetActive (true);
                     DroppedItem.name = HotSlots [SelectedSlotNumber].GetComponent<SlotScript>().ContainedItem.name;
                     DroppedItem.GetComponent<Rigidbody> ().AddForce (Camera.main.transform.forward * 200);
                     if (HotSlots [SelectedSlotNumber].GetComponent<SlotScript>().ContainedItem.GetComponent<ItemScript> ().ItemAmount > 1) {
                         HotSlots [SelectedSlotNumber].GetComponent<SlotScript>().ContainedItem.GetComponent<ItemScript> ().ItemAmount += -1;
                     } else {
                         Destroy (HotSlots [SelectedSlotNumber].GetComponent<SlotScript>().ContainedItem);
                         HotSlots [SelectedSlotNumber].GetComponent<SlotScript>().ContainedItem = null;
                     }
                 }
             }
 
             if (Input.GetMouseButton (0)) {
                 Ray TheRay = new Ray (Camera.main.transform.position, Camera.main.transform.forward);
                 RaycastHit HitResult;
                 if (Physics.Raycast (TheRay, out HitResult, 100)) {
                     if (HitResult.transform.gameObject.GetComponent<BlockScript> () != null) {
                         TargetedBlock = HitResult.transform.gameObject;
                         TargetedBlock.GetComponent<BlockScript> ().Brokeness += Time.deltaTime / HitResult.transform.gameObject.GetComponent<BlockScript> ().Hardness;
                         if (TargetedBlock.GetComponent<BlockScript> ().Brokeness >= 1) {
                             TargetedBlock.GetComponent<BlockScript> ().Break ();
                         }
                     }
                 }
             } else if (TargetedBlock != null) {
                 TargetedBlock.GetComponent<BlockScript> ().Brokeness = 0;
                 TargetedBlock = null;
             }
         } else {
 
             CraftObject ();
 
             if (Input.GetMouseButtonDown (0)) {
                 var Pointer = new PointerEventData (EventSystem.current);
                 Pointer.position = Input.mousePosition;
                 EventSystem.current.RaycastAll (Pointer, Results);
                 if (Results.Count > 0) {
                     var ClickedItem = Results [0].gameObject;
                     if (ClickedItem.GetComponent<SlotScript> () != null) {
                         MoveItemToNewSlot (ClickedItem, MouseSlot);
                     }
                 }
             } else if (Input.GetMouseButtonDown (1)) {
                 var Pointer = new PointerEventData (EventSystem.current);
                 Pointer.position = Input.mousePosition;
                 EventSystem.current.RaycastAll (Pointer, Results);
                 if (Results.Count > 0) {
                     var ClickedItem = Results [0].gameObject;
                     if (ClickedItem.GetComponent<SlotScript> () != null) {
                         MoveOneItemToNewSlot (ClickedItem, MouseSlot);
                     }
                 }
             }
         }
 
         if (Input.GetAxis ("Mouse ScrollWheel") > 0) {
             SelectedSlotNumber += 1;
             if (SelectedSlotNumber > 9) {
                 SelectedSlotNumber = 0;
             }
             SelectionBar.transform.position = HotSlots [SelectedSlotNumber].transform.position;
         }
         if (Input.GetAxis ("Mouse ScrollWheel") < 0) {
             SelectedSlotNumber += -1;
             if (SelectedSlotNumber < 0) {
                 SelectedSlotNumber = 9;
             }
             SelectionBar.transform.position = HotSlots [SelectedSlotNumber].transform.position;
         }
 
         Ray FloorRay = new Ray (transform.position + new Vector3 (0, -1.1f, 0), -transform.up);
         RaycastHit FloorHit;
         if (Physics.Raycast (FloorRay, out FloorHit, 0.5f)) {
             if (FloorHit.transform.gameObject != null) {
                 Grounded = true;
             } else {
                 Grounded = false;
             }
         }
 
         if (Input.GetKeyDown (KeyCode.Space) & Grounded == true) {
             GetComponent<Rigidbody> ().AddForce (transform.up * 15000);
             Grounded = false;
         }
     }
 
     void FixedUpdate(){
         if (Input.GetKey ("w")) {
             transform.position = Vector3.Lerp (transform.position, transform.position + transform.forward, Time.deltaTime * MoveSpeed);
             if (Grounded == true){
                 WalkProgress += 0.05f;
                 if (WalkProgress >= 1) {
                     PlaySound ("Footstep" + Random.Range (1, 4), transform.position, 1, 2);
                     WalkProgress = 0;
                 }
             }
         }
         if (Input.GetKey ("s")) {
             transform.position = Vector3.Lerp (transform.position, transform.position + transform.forward * -0.5f, Time.deltaTime * MoveSpeed);
             if (Grounded == true) {
                 WalkProgress += 0.025f;
                 if (WalkProgress >= 1) {
                     PlaySound ("Footstep" + Random.Range (1, 4), transform.position, 1, 2);
                     WalkProgress = 0;
                 }
             }
         }
         if (Input.GetKey ("d")) {
             transform.position = Vector3.Lerp (transform.position, transform.position + transform.right, Time.deltaTime * MoveSpeed);
         }
         if (Input.GetKey ("a")) {
             transform.position = Vector3.Lerp (transform.position, transform.position + transform.right * -1, Time.deltaTime * MoveSpeed);
         }
     }
 
     void OnCollisionEnter(Collision TheCollision){
         if (TheCollision.gameObject.GetComponent<ItemScript> () != null) {
             var InventoryNumber = 0;
             var SlotFound = false;
             var EmptySlots = new List<GameObject> ();
             foreach (GameObject Slot in AllSlots) {
                 if (Slot.GetComponent<SlotScript> ().ContainedItem != null) {
                     if (Slot.GetComponent<SlotScript> ().ContainedItem.name == TheCollision.gameObject.name) {
                         ChangeItemAmount (Slot.GetComponent<SlotScript> ().ContainedItem, TheCollision.gameObject.GetComponent<ItemScript> ().ItemAmount);
                         Destroy (TheCollision.gameObject);
                         PlaySound ("Pop", transform.position, 1, 3);
                         SlotFound = true;
                         break;
                     }
                 } else {
                     EmptySlots.Add (Slot);
                 }
             }
             if (SlotFound == false) {
                 if (EmptySlots.Count > 0) {
                     EmptySlots [0].GetComponent<SlotScript> ().ContainedItem = TheCollision.gameObject;
                     TheCollision.gameObject.SetActive (false);
                     PlaySound ("Pop", transform.position, 1, 3);
                 } else {
                     InventoryFull = true;
                 }
             }
         }
     }
             
 
     void ChangeItemAmount(GameObject Item, int Difference){
         Item.GetComponent<ItemScript> ().ItemAmount += Difference;
         if (Item.GetComponent<ItemScript> ().ItemAmount < 1) {
             Destroy (Item);
         }
     }
 
     void CraftObject(){
         var i = 0;
         var CraftingString = "";
         foreach (GameObject Slot in CraftingGridSlots) {
             if (CraftingGridSlots [i].GetComponent<SlotScript> ().ContainedItem != null) {
                 CraftingString += CraftingGridSlots [i].GetComponent<SlotScript> ().ContainedItem.GetComponent<ItemScript> ().name;
             } else {
                 CraftingString += "Empty";
             }
             i += 1;
         }
         foreach (GameObject Object in Objects) {
             if (CraftingString == Object.GetComponent<ItemScript> ().CraftingRecipe) {
                 if (OutputSlot.GetComponent<SlotScript> ().ContainedItem == null) {
                     var CraftedItem = Instantiate (Object, Camera.main.transform.position + Camera.main.transform.forward, Quaternion.identity);
                     CraftedItem.GetComponent<ItemScript> ().ItemAmount = 1;
                     CraftedItem.name = Object.name;
                     CraftedItem.SetActive (false);
                     OutputSlot.GetComponent<SlotScript> ().ContainedItem = CraftedItem;
                 }
             } else {
                 if (OutputSlot.GetComponent<SlotScript> ().ContainedItem != null) {
                     OutputSlot.GetComponent<SlotScript> ().ContainedItem = null;
                 }
                 Debug.Log (CraftingString);
                 Debug.Log(Object.GetComponent<ItemScript> ().CraftingRecipe);
             }
         }
     }
 
     public void WipeGrid(){
         var i = 0;
         foreach (GameObject Slot in CraftingGridSlots){
             if (Slot.GetComponent<SlotScript> ().ContainedItem != null) {
                 ChangeItemAmount (Slot.GetComponent<SlotScript>().ContainedItem, -1);
             }
             i += 1;
         }
     }
 
     public void MoveItemToNewSlot(GameObject OriginalSlot, GameObject NewSlot){
         if (MouseSlot.GetComponent<SlotScript> ().ContainedItem == null) {
             if (OriginalSlot == OutputSlot) {
                 WipeGrid();
             }
         }
         if (NewSlot.GetComponent<SlotScript> ().ContainedItem == null) {
             NewSlot.GetComponent<SlotScript> ().ContainedItem = OriginalSlot.GetComponent<SlotScript> ().ContainedItem;
             OriginalSlot.GetComponent<SlotScript> ().ContainedItem = null;
         } else {
             if (OriginalSlot.GetComponent<SlotScript>().ContainedItem != null){
                 if (OriginalSlot.GetComponent<SlotScript> ().ContainedItem.name == NewSlot.GetComponent<SlotScript> ().ContainedItem.name) {
                     NewSlot.GetComponent<SlotScript> ().ContainedItem.GetComponent<ItemScript> ().ItemAmount += OriginalSlot.GetComponent<SlotScript> ().ContainedItem.GetComponent<ItemScript> ().ItemAmount;
                     Destroy (OriginalSlot.GetComponent<SlotScript> ().ContainedItem);
                 }
             }
             var OldItem = NewSlot.GetComponent<SlotScript> ().ContainedItem;
             NewSlot.GetComponent<SlotScript> ().ContainedItem = OriginalSlot.GetComponent<SlotScript> ().ContainedItem;
             OriginalSlot.GetComponent<SlotScript> ().ContainedItem = OldItem;
         }
     }
     public void MoveOneItemToNewSlot(GameObject OriginalSlot, GameObject NewSlot){
         if (NewSlot.GetComponent<SlotScript> ().ContainedItem == null) {
             if (OriginalSlot.GetComponent<SlotScript> ().ContainedItem != null){
                 if (OriginalSlot.GetComponent<SlotScript> ().ContainedItem.GetComponent<ItemScript> ().ItemAmount > 1) {
                     var NewItem = Instantiate (OriginalSlot.GetComponent<SlotScript> ().ContainedItem, Vector3.zero, Quaternion.identity);
                     NewItem.GetComponent<ItemScript> ().ItemAmount = 1;
                     NewItem.name = OriginalSlot.GetComponent<SlotScript> ().ContainedItem.name;
                     OriginalSlot.GetComponent<SlotScript> ().ContainedItem.GetComponent<ItemScript> ().ItemAmount += -1;
                     NewSlot.GetComponent<SlotScript> ().ContainedItem = NewItem;
                 }
             }
         }
     }
 }
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cherno · Apr 15, 2018 at 04:16 PM 0
Share

You could write sub-classes and have an instance of them in your main script. If the classes need an Updata method, just give them one and call it from your main class' Update method. If the only concern is readability, remember that in Visual Studio, you can collapse methods and also navigate not just by scrolling through the whole script but also by selecting namespaces and methods from thedrop-down fields in the bar above the main script view.

avatar image Davidblackberry1 Cherno · Apr 15, 2018 at 05:35 PM 0
Share

It's more of just the fact that as I make more and more progress, the script gets increasingly complex and I might not even be able to understand what's going on at some point.

avatar image BastianUrbach Davidblackberry1 · Apr 15, 2018 at 07:39 PM 0
Share

Comments are key to keep it understandable. Some scripts just grow big, that's not inherently wrong. You can also use #region and #endregion to have several visually separated and foldable regions in the code. Of course splitting it into multiple scripts is also a good idea, I would at least try to separate inventory related and movement related code.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SunnyChow · Apr 16, 2018 at 03:16 AM

i suggest you separate the script to two components: user control, game mechanic. For example, the user control component do ray cast to get the block, and call a function of game mechanic component so that the game mechanic component can deal with the block. Or, the user control component detect key press, and call the move function of game mechanic component.

The benefit is that, it's easier for you to change the control in the future, in you want to make the game also support game pad or touch screen, because you can just change the user control component .

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

140 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

FPS animation problem! 1 Answer

SMOOTH ROTATE ON JUMP?????????,Smooth Rotation On Jump????????? 1 Answer

Change a variable name only on the Inspector 2 Answers

Problem with script not updating to game? 2 Answers

Box collider that triggers camera change 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges