Hi im having the error The name `currentFramePosition' does not exist in the current context i can't find a solution can someone help me?
Basicly the title says everything this issue is repeted in lines: 32,70/47,24/53,36/55,36/85,39
File where the issue is linked to using System.Collections; using System.Collections.Generic; using UnityEngine;
public class MouseController : MonoBehaviour {
public GameObject CircularCursor;
Vector3 lastFramePosition;
Vector3 dragStartPosition;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 currentFramePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
currentFramePosition.z = 0;
UpdateCursor();
UpdateDragging();
UpdateCameraMovement();
lastFramePosition = Camera.main.ScreenToWorldPoint( Input.mousePosition );
lastFramePosition.z = 0;
}
void UpdateCursor() {
// Update the circle cursor position
Tile tileUnderMouse = WorldController.Instance.GetTileAtWorldCoord(currentFramePosition);
if(tileUnderMouse != null) {
CircularCursor.SetActive(true);
Vector3 cursorPosition = new Vector3(tileUnderMouse.X, tileUnderMouse.Y, 0);
CircularCursor.transform.position = cursorPosition;
}
else {
// Mouse is outside of the valid tile space, so hide the cursor.
CircularCursor.SetActive(false);
}
}
void UpdateDragging() {
// Start Drag
if( Input.GetMouseButtonDown(0) ) {
dragStartPosition = currentFramePosition;
}
// End Drag
if( Input.GetMouseButtonUp(0) ) {
int start_x = Mathf.FloorToInt( dragStartPosition.x );
int end_x = Mathf.FloorToInt( currentFramePosition.x );
int start_y = Mathf.FloorToInt( dragStartPosition.y );
int end_y = Mathf.FloorToInt( currentFramePosition.y );
// We may be dragging in the "wrong" direction, so flip things if needed.
if(end_x < start_x) {
int tmp = end_x;
end_x = start_x;
start_x = tmp;
}
if(end_y < start_y) {
int tmp = end_y;
end_y = start_y;
start_y = tmp;
}
// Loop through all the tiles
for (int x = start_x; x <= end_x; x++) {
for (int y = start_y; y <= end_y; y++) {
Tile t = WorldController.Instance.world.GetTileAt(x, y);
if(t != null) {
t.Type = Tile.TileType.Grass;
}
}
}
}
}
void UpdateCameraMovement() {
// Handle screen panning
if( Input.GetMouseButton(1) || Input.GetMouseButton(2) ) { // Right or Middle Mouse Button
Vector3 diff = lastFramePosition - currentFramePosition;
Camera.main.transform.Translate( diff );
}
}
}
Thanks in advanced
$$anonymous$$oved to the help room as this question is a trivial problem which is based on lacking of understanding basic program$$anonymous$$g principles.
Answer by Bunny83 · Jan 12, 2017 at 05:26 AM
You declared your currentFramePosition
variable as a local variable inside your Update method:
void Update () {
Vector3 currentFramePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
That means the variable only exists during the execution of the Update method and is not available anywhere else.
You most likely want to declare the variable inside your class as member variable:
Vector3 currentFramePosition;
void Update () {
currentFramePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
Your answer
Follow this Question
Related Questions
Mouse-Keyboard Not Working - New User 1 Answer
Rotating object on z axis only when following mouse movements. 0 Answers
Can't Get Prefab Clone To Follow Mouse 2 Answers
Draggable Door Problems 0 Answers