Use of unassigned local variable 'startPos' " & " Use of unassigned local variable 'endPos'
Could someone explain why i get an error on line 78 - " Use of unassigned local variable 'startPos' " & " Use of unassigned local variable 'endPos' ".
Can you not pass variables to another method this way? or am I miss understanding how to do this.
using UnityEngine;
public class PlayerInput : MonoBehaviour
{
[SerializeField] readonly float swipeDeadzone = 125f;
PlayerMovement playerMovement;
Pewpews pewpews;
UIController uIController;
GameObject player;
void Start()
{
playerMovement = player.GetComponent<PlayerMovement>();
pewpews = player.GetComponentInChildren<Pewpews>();
uIController = GameObject.Find("UIController").GetComponent<UIController>();
player = GameObject.FindGameObjectWithTag("Player");
}
void Update()
{
GetPlayerInput();
}
void GetPlayerInput()
{
Vector3 startPos;
Vector3 endPos;
if (uIController.menuIsActive == true)
{
return;
}
#region Standalone Controls
// Standalone Controls are only used in the editor, as touch / swipe is considered mouse input even on mobile platform.
if (Application.isEditor)
{
if (Input.GetMouseButtonDown(0))
{
startPos = Input.mousePosition;
}
if (Input.GetMouseButtonUp(0))
{
endPos = Input.mousePosition;
}
}
#endregion
#region Touch Controls
// Track a single touch as a direction control.
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
// Handle finger movements based on touch phase.
switch (touch.phase)
{
// Record initial touch position.
case TouchPhase.Began:
startPos = touch.position;
break;
// Report that a direction has been chosen when the finger is lifted.
case TouchPhase.Ended:
endPos = touch.position;
break;
}
}
#endregion
MovePlayer(startPos, endPos);
}
void MovePlayer(Vector3 startPos, Vector3 endPos)
{
Vector3 swipeDirection;
if (startPos != Vector3.zero && endPos != Vector3.zero)
{
swipeDirection = endPos - startPos;
if (swipeDirection.magnitude > swipeDeadzone)
{
if (Mathf.Abs(swipeDirection.x) > Mathf.Abs(swipeDirection.y))
{
playerMovement.PlayerRotationDirection(swipeDirection);
ResetPos(startPos, endPos);
}
}
else
{
pewpews.Fire(endPos);
ResetPos(startPos, endPos);
}
}
}
void ResetPos(Vector3 startPos, Vector3 endPos)
{
startPos = endPos = Vector3.zero;
}
}
And I don't see any purpose for the methods ResetPos() as you create startPos and endPos variables each frame at the start of GetPlayerInput(). It would have a sense if you have declared them as class members available for all methods, ins$$anonymous$$d of inside GetPlayerInput()...
Answer by misher · Feb 26, 2019 at 08:14 AM
You assign your variables inside if statements, and there can be a case when no one of those is executed ( and you don't have the final "else" where you also assign values to your variables). So you can end up calling MovePlayers() with variables not assigned. The simple solution to this can be the following - assign default values to your variables when you initialize them:
Vector3 startPos = Vector3.zero;
Vector3 endPos = Vector3.zero;