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

Optional parameters with default values

Discussion in 'Scripting' started by CG_Echtzeitschmiede, Feb 28, 2015.

  1. CG_Echtzeitschmiede

    CG_Echtzeitschmiede

    Joined:
    Feb 19, 2015
    Posts:
    93
    Hello,

    I like how you can expose variables in the Unity editor. But now I have a problem with default parameters. In the below code, I have a public method "Grow", which I want to be able to call from other scripts. When I call it, I want to be able to call with:
    Grow(Time.time);
    and have the function use default parameters for starting size, end size and duration. But I want these default parameters to be specified in Unity editor. Is my below workaround how this is usually done??

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GrowObject : MonoBehaviour
    5. {
    6.     public float standardDuration = 0.2f;    // These are set in Unity editor
    7.     public float startSize = 0f;                    // These are set in Unity editor
    8.     public float stopSize = 1f;                    // These are set in Unity editor
    9.     private float startTime;
    10.     private float stopTime;
    11.     private float currentSize;
    12.     private bool doGrow = false;
    13.  
    14.     public void Start()
    15.     {
    16.         Grow(Time.time);
    17.     }
    18.  
    19.     // I am not allowed to do public void Grow(float start, float duration = standardDuration etc.)
    20.     public void Grow(float start, float size1 = -1000001f, float size2 = -1000001f, float duration = -1f)
    21.     {
    22.         if (duration < 0)
    23.             duration = standardDuration;
    24.         if (size1 > 1000000)
    25.             startSize = size1;
    26.         if (size2 > 1000000)
    27.             stopSize = size2;
    28.         doGrow = true;
    29.         startTime = start;
    30.         stopTime = startTime + duration;
    31.     }
    32.    
    33.     void Update ()
    34.     {
    35.         if (doGrow)
    36.         {
    37.             if (Time.time > stopTime)
    38.             {
    39.                 doGrow = false;
    40.                 return;
    41.             }      
    42.         currentSize = Mathf.Lerp (startSize, stopSize, Mathf.InverseLerp (startTime, stopTime, Time.time));
    43.         gameObject.transform.localScale = new Vector3 (currentSize, currentSize, currentSize);
    44.         }
    45.     }
    46. }
     
  2. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Why not make a second method that overloads the original Grow method? That is the way it is usually done.

    Code (CSharp):
    1. public void Grow(float start)
    2. {
    3.     // You can insert the default fields here too
    4.     Grow(start,  -1000001f,  -1000001f, -1f);
    5. }
    6.  
    7. public void Grow(float start, float size1, float size2, float duration)
    8. {
    9.     // Implementation
    10. }
    http://csharp.net-tutorials.com/classes/method-overloading/
     
    Last edited: Feb 28, 2015
  3. CG_Echtzeitschmiede

    CG_Echtzeitschmiede

    Joined:
    Feb 19, 2015
    Posts:
    93
    I see, that makes way more sense than what I attempted. Thank you very much!
     
  4. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    Masticater likes this.