Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

how to get return value

Discussion in 'Scripting' started by kumar123k, Jul 12, 2016.

  1. kumar123k

    kumar123k

    Joined:
    Jul 5, 2016
    Posts:
    25
    Hi i am trying to get distance

    Code (CSharp):
    1. publicfloatGetTotalLength(){
    2. if(vectorPath==null)returnfloat.PositiveInfinity;
    3. floattot=0;
    4. for(inti=0;i<vectorPath.Count-1;i++)tot+=Vector3.Distance(vectorPath,vectorPath[i+1]);
    5. returntot;
    6. }


    i am trying to get value from the above function how to get the return value to the another script.
     
    Last edited: Jul 12, 2016
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,694
    please...use code tags
    http://forum-old.unity3d.com/threads/using-code-tags-properly.143875/

    and also...proper spacing. everything runs together.

    otherwise, to get the return value, you just assign the method call to a variable, or you use the method directly.

    float returnValue = GetTotalLength();
    or float addValue = 1.5f + GetTotalLength();
     
  3. kumar123k

    kumar123k

    Joined:
    Jul 5, 2016
    Posts:
    25
    i am trying to access from another class so i dont know how to do that.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,694
    You have to have a reference to that script and call that method from one script to the other.

    so
    float returnValue = myScriptWIthMethod.GetTotalLength();

    Depending on how your scripts are setup depends on how you'd create that reference.
     
  5. kumar123k

    kumar123k

    Joined:
    Jul 5, 2016
    Posts:
    25
    i am using remainingdistance as the float variable.
    // path was class
    Path currentlength;
    remainingdistance = currentlength.GetTotalLength();

    when i use like this i am getting error
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    4,694
    List errors. But chances are you are getting a null error there.
    Also doesn't hurt to show your code for the part that is getting the value.
     
  7. answerwinner

    answerwinner

    Joined:
    Jan 8, 2015
    Posts:
    47
    Original code
    Code (CSharp):
    1.  
    2. public float GetTotalLength()
    3. {
    4.     if (vectorPath == null)
    5.         return float.PositiveInfinity;
    6.  
    7.     float tot = 0;
    8.     for (int i = 0; i < vectorPath.Count - 1; i++)
    9.         tot += Vector3.Distance(vectorPath, vectorPath[i + 1]);
    10.  
    11.     return tot;
    12. }
    13.