Search Unity

Use of unassigned local variable

Discussion in 'Scripting' started by Megalosaurus, May 28, 2015.

  1. Megalosaurus

    Megalosaurus

    Joined:
    May 12, 2013
    Posts:
    16
    Hi there!

    I'm an artist who is trying to learn a little scripting to test my assets, I'm super new to it. A lot of the code is actually borrowed from elsewhere! I'd really appreciate some help with this problem.

    Everything works when played in Unity, but i get the following error when i try to make a build:

    (39/44): error CS0165: Use of unassigned local variable 'ray'

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerMovement : MonoBehaviour {
    5.     //flag to check if the user has tapped / clicked.
    6.     //Set to true on click. Reset to false on reaching destination
    7.     private bool flag = false;
    8.     //destination point
    9.     private Vector3 endPoint;
    10.     //alter this to change the speed of the movement of player / gameobject
    11.     public float duration = 50.0f;
    12.     private Animator anim;
    13.     public Vector3 startPosition;
    14.    
    15.     void Start(){
    16.         startPosition = transform.position;
    17.         anim = GetComponent<Animator>();
    18.     }
    19.    
    20.     // Update is called once per frame
    21.     void Update () {
    22.  
    23.         //check if the screen is touched / clicked  
    24.         if((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) || (Input.GetMouseButtonDown(0)))
    25.         {
    26.             //declare a variable of RaycastHit struct
    27.             RaycastHit hit;
    28.             //Create a Ray on the tapped / clicked position
    29.             Ray ray;
    30.             //for unity editor
    31.             #if UNITY_EDITOR
    32.             ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    33.             //for touch device
    34.             #elif (UNITY_ANDROID || UNITY_IPHONE || UNITY_WP8)
    35.             ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
    36.             #endif
    37.            
    38.             //Check if the ray hits any collider
    39.             if(Physics.Raycast(ray, out hit))
    40.             {
    41.                 //set a flag to indicate to move the gameobject
    42.                 flag = true;
    43.                 //save the click / tap position
    44.                 endPoint = hit.point;
    45.                 Debug.Log(endPoint);
    46.                 anim.SetFloat("SpeedX", endPoint.x);
    47.                 anim.SetFloat("SpeedY", endPoint.y);
    48.             }              
    49.  
    50.         }
    51.         //check if the flag for movement is true and the current gameobject position is not same as the clicked / tapped position
    52.         if(flag && !Mathf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude)){ //&& !(V3Equal(transform.position, endPoint))){
    53.             //move the gameobject to the desired position
    54.             gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, endPoint, 1/(duration*(Vector3.Distance(gameObject.transform.position, endPoint))));
    55.         }
    56.         //set the movement indicator flag to false if the endPoint and current gameobject position are equal
    57.         else if(flag && Mathf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude)) {
    58.             flag = false;
    59.             Debug.Log("I am here");
    60.         }
    61.         {
    62.             if(flag && !Mathf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude)) {
    63.                 anim.SetBool("walking", true);
    64.             } else if (flag && Mathf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude)) {
    65.                 anim.SetBool("walking", false);
    66.                     }
    67.        
    68.         }
    69.  
    70.     }
    71. }
    72.  
     
  2. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    I believe your conditional compilation means that the compiler can't be sure that ray has been assigned a value before it is used in line 39.

    Changing line 29 to
    Code (CSharp):
    1. Ray ray = null;
    should allow it to compile, however you should really change your conditional compilation block to have a default part which also initializes the variable ray, to guard against possible run time errors.
     
  3. Megalosaurus

    Megalosaurus

    Joined:
    May 12, 2013
    Posts:
    16
    Thanks Munchy2007

    I tried out your suggestion and got the following error:

    CS0037: Cannot convert null to 'UnityEngine.Ray' because it is a value type

    As for your other suggestion, I'm still too new to this to understand what you mean :p I need to spend some more time doing the basic tutorials.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    A Ray is a struct, and cannot be null.

    Your problem is probably that your #if chain does not have a 'default' condition, and though I'm not familiar with all the preprocessor directives, I'd bet that you're building in a situation where none of those are true. I'd restructure it so that the "using mouse" code is the default fallback, rather than being specific to the editor:
    Code (csharp):
    1.  
    2. #if (UNITY_ANDROID || UNITY_IPHONE || UNITY_WP8)
    3.             ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
    4.             #else
    5.             ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    6.             #endif
    7.  
     
  5. Megalosaurus

    Megalosaurus

    Joined:
    May 12, 2013
    Posts:
    16
    Yay! It will build now :D

    Thanks very much StarManta!
     
  6. _Abishek_

    _Abishek_

    Joined:
    Sep 9, 2015
    Posts:
    2
    if((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) || (Input.GetMouseButtonDown(0)))
    {
    //rb.AddForce(transform.up * thrust);
    //declare a variable of RaycastHit struct
    RaycastHit hit;
    //Create a Ray on the tapped / clicked position
    Ray ray;

    #if (UNITY_ANDROID || UNITY_IPHONE || UNITY_WP8)
    ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
    #else
    ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    #endif
    //Check if the ray hits any collider
    if(Physics.Raycast(ray,out hit))
    {
    //set a flag to indicate to move the gameobject
    flag = true;
    //save the click / tap position
    endPoint = hit.point;
    //as we do not want to change the y axis value based on touch position, reset it to original y axis value
    endPoint.y = yAxis;
    }

    }
    //check if the flag for movement is true and the current gameobject position is not same as the clicked / tapped position
    if(flag && !Mathf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude)){ //&& !(V3Equal(transform.position, endPoint))){
    //move the gameobject to the desired position
    gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, endPoint, 4/(duration*(Vector3.Distance(gameObject.transform.position, endPoint))));
    }
    //set the movement indicator flag to false if the endPoint and current gameobject position are equal
    else if(flag && Mathf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude)) {
    flag = false;
    Debug.Log("I am here");
    }
    }
     
  7. _Abishek_

    _Abishek_

    Joined:
    Sep 9, 2015
    Posts:
    2
    i am getting the same error pls someone help me

    error CS0165: Use of unassigned local variable 'ray' : 15th line