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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Optimize my script

Discussion in 'Scripting' started by Shayke, Jan 25, 2018.

  1. Shayke

    Shayke

    Joined:
    Dec 8, 2017
    Posts:
    352
    Hi, sometimes i think i write my code not in the right way.
    For example, i want one of the object to appear slowly:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ObjectAppear : MonoBehaviour {
    6.     private float Alpha;
    7.  
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.         Alpha = 0;
    12.        
    13.  
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update () {
    18.         GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, Alpha);
    19.         if(Alpha < 1)
    20.         {
    21.             Alpha += 0.9f * Time.deltaTime;
    22.         }
    23.      
    24.     }
    25. }
    26.  
    So this is the code and its working, But the thing is the code keep repeating himself.
    There is any other way to prevent that code from repeating? Any function that destroy the script and not the object.
    Thanks!
     
  2. Prastiwar

    Prastiwar

    Joined:
    Jul 29, 2017
    Posts:
    125
    Don't use "GetComponent" every frame, cache it in Awake and then reference to it.
    You can simply use Destroy(this); to remove just script if you won't use it again..
     
  3. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    My script would probably look something like this:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ObjectAppear : MonoBehaviour
    6. {
    7.     // Alpha to start with
    8.     private float Alpha = 0;
    9.  
    10.     // Reference to our SpriteRenderer, fetched once with GetComponent in Awake
    11.     private SpriteRenderer spriteRenderer;
    12.  
    13.     // bool so this script knows if it still needs to update the alpha of the sprite
    14.     private bool isTransitioningAlpha = true;
    15.  
    16.     void Awake()
    17.     {
    18.         // Get the SpriteRenderer attached to this GameObject
    19.         spriteRenderer = GetComponent<SpriteRenderer>();
    20.  
    21.         if(spriteRenderer == null)
    22.             Debug.LogError("GameObject does not have a SpriteRenderer component!", this);
    23.     }
    24.  
    25.     void Update ()
    26.     {
    27.         // If we are at max alpha, we do not need to continue to assign a new Color.
    28.         // Its not changing
    29.         if(isTransitioningAlpha == false)
    30.             return;
    31.  
    32.         // Alpha is not max yet, increment it some amount and assign it to the spriterenderer
    33.         if(Alpha < 1)
    34.         {
    35.             Alpha += 0.9f * Time.deltaTime;
    36.             spriteRenderer.color = new Color(1, 1, 1, Alpha);
    37.         }
    38.         else
    39.         {
    40.             // Alpha is greater than or equal to 1. Transition is finished
    41.             isTransitioningAlpha = false;
    42.  
    43.             // ALTERNATIVELY:
    44.             // This could be where you destroy this component. Which would probably be better.
    45.         }    
    46.     }
    47. }
    48.  
    This is so we are not continually assigning a new color to the spriterenderer unless the alpha has changed
     
    DonLoquacious likes this.
  4. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    And just to be contrary, mine would look like this:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SpriteVisibility : MonoBehaviour
    6. {
    7.     public enum Visibility
    8.     {
    9.         Opaque,
    10.         Transparent
    11.     };
    12.  
    13.     [SerializeField]
    14.     [Range(.01f, 10f)]
    15.     private float _transitionTime = 1f;
    16.  
    17.     [SerializeField]
    18.     private bool _automaticStart = true;
    19.  
    20.     [SerializeField]
    21.     private Visibility _startingVisibility = Visibility.Opaque;
    22.  
    23.     [SerializeField]
    24.     private Visibility _targetVisibility = Visibility.Transparent;
    25.  
    26.  
    27.     private bool _transitioning;
    28.     private float _currentAlpha;
    29.     private float _targetAlpha;
    30.     private SpriteRenderer _spriteRenderer;
    31.  
    32.  
    33.     private void Awake()
    34.     {
    35.         _spriteRenderer = GetComponent<SpriteRenderer>();
    36.  
    37.         _currentAlpha = _startingVisibility == Visibility.Opaque ? 1f : 0f;
    38.         _spriteRenderer.color = new Color(_spriteRenderer.color.r, _spriteRenderer.color.g, _spriteRenderer.color.b, _currentAlpha);
    39.     }
    40.  
    41.     private void Start()
    42.     {
    43.         if (_automaticStart && !_transitioning && _targetVisibility != _startingVisibility)
    44.             StartCoroutine(Transition());
    45.     }
    46.  
    47.  
    48.     public void ChangeVisibility(Visibility newVisibility, float transitionTime)
    49.     {
    50.         if (_transitionTime > 0f)
    51.             _transitionTime = transitionTime;
    52.  
    53.         ChangeVisibility(newVisibility);
    54.     }
    55.  
    56.     public void ChangeVisibility(Visibility newVisibility)
    57.     {
    58.         if (_targetVisibility != newVisibility)
    59.         {
    60.             _targetVisibility = newVisibility;
    61.  
    62.             if (!_transitioning)
    63.                 StartCoroutine(Transition());
    64.         }
    65.     }
    66.  
    67.     private IEnumerator Transition()
    68.     {
    69.         _transitioning = true;
    70.  
    71.         _targetAlpha = _targetVisibility == Visibility.Opaque ? 1 : 0;
    72.         _currentAlpha = _spriteRenderer.color.a;
    73.  
    74.         while (_currentAlpha != _targetAlpha)
    75.         {
    76.             yield return null;
    77.  
    78.             _targetAlpha = _targetVisibility == Visibility.Opaque ? 1 : 0;
    79.             _currentAlpha = Mathf.MoveTowards(_currentAlpha, _targetAlpha, Time.deltaTime / _transitionTime);
    80.  
    81.             _spriteRenderer.color = new Color(_spriteRenderer.color.r, _spriteRenderer.color.g, _spriteRenderer.color.b, _currentAlpha);
    82.         }
    83.         _transitioning = false;
    84.     }
    85. }
     
    Last edited: Jan 25, 2018
    TaleOf4Gamers likes this.
  5. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Why the boolean at all? It's redundant. :p
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    When it reaches your target alpha, you could set enabled to false. Just another option.
     
    TaleOf4Gamers likes this.
  7. Shayke

    Shayke

    Joined:
    Dec 8, 2017
    Posts:
    352
    I think the answer was to use IEnumerator ?
    I just want my game to run faster, i need to avoid all those codes that i dont need.
    I needed the Function just once, but as you see the script ask the question again and again until the object destroyed.
    What i wanted is to remove just the Script Component.
    Destroy(this) is destroying also the object no?

    Edit:
    this was the answer
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ObjectAppear : MonoBehaviour {
    6.     private float Alpha;
    7.  
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.         Alpha = 0;
    12.        
    13.  
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update () {
    18.         GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, Alpha);
    19.         if (Alpha < 1)
    20.         {
    21.             Alpha += 0.9f * Time.deltaTime;
    22.         }
    23.         else Destroy(GetComponent<ObjectAppear>());
    24.     }
    25. }
    26.  
     
    Last edited: Jan 25, 2018
  8. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,186
    Nope. 'this' refers to the script instance, so only that will get Destroyed. Destroy(gameObject) is what destroys the GameObject the script is attached to.
     
  9. Prastiwar

    Prastiwar

    Joined:
    Jul 29, 2017
    Posts:
    125
    Nope, it's "this" - script, If you would destroy gameobject u would write Destroy((this.)gameObject);
     
  10. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    I get the impression your guessing... don't guess. Find out what elements are causing any slow down you have, then work on those elements. Since this is a really simple script I doubt it causing you that much bother.

    https://docs.unity3d.com/Manual/ProfilerWindow.html
    https://unity3d.com/learn/tutorials/temas/performance-optimization/profiler-window
     
    TaleOf4Gamers likes this.
  11. Shayke

    Shayke

    Joined:
    Dec 8, 2017
    Posts:
    352
  12. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    You are still calling GetComponent every frame, dont. Call it once in Awake and cache it. This is the single most intense part in the script. (Relative of course, theres not a lot else going on)