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

How can I optimize this Script?

Discussion in 'Scripting' started by Kingdomkey6677, Nov 21, 2018.

  1. Kingdomkey6677

    Kingdomkey6677

    Joined:
    Mar 27, 2017
    Posts:
    2
    I'm still very new to scripting. I know that in anything more serious this script optimization of a script this small is inconsequential. So, what I'm really asking for here is tips for the optimization of all scripts in general, and using this as an example.
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class Swayer : MonoBehaviour {
    5.  
    6.     public GameObject SwayObject;
    7.     public AudioSource Sounds;
    8.     public Text clickNum;
    9.     public Text highScore;
    10.  
    11.     private int clickCount;
    12.  
    13.     void Start()
    14.     {
    15.         highScore.text = PlayerPrefs.GetInt("highScore", 0).ToString();
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update() {
    20.         clickNum.text = clickCount.ToString();
    21.  
    22.         if (Input.GetButtonDown("Fire1")) {
    23.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    24.             RaycastHit hit;
    25.  
    26.             if (Physics.Raycast(ray, out hit, 50))
    27.             {
    28.                 Instantiate(SwayObject, hit.point, transform.rotation);
    29.                 Sounds.Play();
    30.                 clickCount++;
    31.                 if (clickCount > PlayerPrefs.GetInt("highScore", 0)) {
    32.                     PlayerPrefs.SetInt("highScore", clickCount);
    33.                     highScore.text = clickCount.ToString();
    34.                 }
    35.             }
    36.         }
    37.     }
    38.  
    39.     public void ResetHighScore() {
    40.         PlayerPrefs.SetInt("highScore", 0);
    41.         highScore.text = PlayerPrefs.GetInt("highScore", 0).ToString();
    42.     }
    43. }
    44.  
    One Idea I've had is to store
    PlayerPrefs.GetInt("highScore", 0))
    in a variable, so it doesn't have to check it so often. However, I don't know how to do this and have my script function the same way, or if that would work at all.

    Again I'm unsure if this script even has the ability to be optimized (or if "optimized" is really the word of looking for). I'm just looking for tips to make good habits as my scripts get more complicated.

    Thanks in advance!
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,903
  3. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    Why do you feel this code needs to be optimized?

    Is your FPS unacceptable? Are you getting massive memory spikes? Jerky camera movement?

    If the answer to all the above questions is no, then you don't need to optimize. Your end users aren't going to know or even care about whether you used (or avoided) Linq, or used inline functions, or cached variables, etc., etc., etc.

    Get your stuff working first. Then profile your code. Identify the bottlenecks. THEN focus on optimizing out those bottlenecks.

    Nothing kills productivity faster than premature optimization*

    *I'm sure wise guys will dispute this, but that's not really the point. :p
     
    rahulk1991, Ryiah and hippocoder like this.
  4. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,903
    I dispute! :D
    Watching videos and reading articles all day long about optimization instead of making your game kills faster.
     
    rahulk1991, Ryiah and BlackPete like this.
  5. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,574
    Second it.

    So what is the point of ECS, if not optimization ?:) (just a rhetorical question)
    Good thing about "premature" optimization in general, you learn about optimal methods and alternatives from day one and saving later a lot of time. It is near impossible to effectively optimize game, which is near release date.
     
  6. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,903
    Well, technically ECS isn't optimization, like neither MVC nor OOD is. It's a paradigm and you're not doing it to optimize your otherwise working code base, you're building your code base with it. #smartassasusual
     
    rahulk1991 and Antypodish like this.
  7. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,574
    I hope slap myslef that label one day ;)

    True, specially second halve. Yet people doing Unity ECS, with performance gain in mind. Is not just because we are more familiar, or is more convenient. For most of us is extra step to figure it out. At least at beginning.
     
  8. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,903
    Feel free. Just try it, satisfactory and it's free! :D

    Of course, but this is true to every architectural decisions in software development. We always optimize toward something. If you choose MVC and OOP you're (prematurely - hehe) optimizing towards easy maintainability (any coding monkey can read your code and be able to change it without deep understanding of the domain and the underlying architecture), if you choose DOD and ECS, you're doing the same towards performance (no matter if it's a game or not).

    So in my opinion ECS isn't premature optimization if we want to keep the term's meaning at bay.
     
    Last edited: Nov 21, 2018
    Antypodish likes this.
  9. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,574
    Actually I like this example comparison. I think it is an interesting view angle at the topic.