- Home /
wierd not existing in current context error caused by 2 lines of code
Hi guys I have the errors
Assets/Code/FPS_Controller.cs(70,37): error CS0103: The name `moveVert' does not exist in the current context
and
Assets/Code/FPS_Controller.cs(70,51): error CS0103: The name `moveHor' does not exist in the current context
but i am not at all referencing these variables anywhere except where they were created witch is fixedUpdate, and even less in Update where theese errors are pointing (but they apeared once i moved the Jump code into Update)
Here is my simple fps conntroller script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FPS_Controller : MonoBehaviour {
public float Accellaration;
public float maxSpeed;
public float JumpHeight;
public float MouseSpeed = 3;
public GameObject Camera;
//public GameObject parent;
public float Zoffset= 0;
private float LookAngleX = 0;
private float LookAngleY = 0;
private Rigidbody rb;
private Animator anim;
private Vector3 movement;
private Vector2 XYmovement;
private float Jump;
// Use this for initialization
void Start () {
rb = gameObject.GetComponent<Rigidbody>();
anim = GetComponent<Animator> ();
}
void FixedUpdate (){
float moveVert = Input.GetAxis ("Vertical");
float moveHor = Input.GetAxis ("Horizontal");
XYmovement = new Vector2 (rb.velocity.x, rb.velocity.z);
if( XYmovement.magnitude > maxSpeed)// clamping speed to max speed
{
XYmovement = XYmovement.normalized * maxSpeed;
rb.velocity = new Vector3 (XYmovement.x, rb.velocity.y, XYmovement.y);
}
movement = new Vector3 (moveVert, Jump, -moveHor);
movement = transform.TransformDirection (movement);
rb.AddForce (movement*Accellaration);
// Taking controller mouse input
var Xin = Input.GetAxis ("Mouse X");
var Yin = Input.GetAxis ("Mouse Y");
LookAngleX += Xin * MouseSpeed;
LookAngleY += Yin * MouseSpeed;
LookAngleY = Mathf.Clamp (LookAngleY, -25f, 89f);
Camera.transform.localEulerAngles = new Vector3(-LookAngleY, Zoffset, 0f);
transform.eulerAngles = new Vector3(0f, LookAngleX, 0f);
//transform.eulerAngles = new Vector3(0f, LookAngleX, 0f);
Jump = 0f;
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Jump")) {
Jump = JumpHeight;
}//############################## this is the line in wich both errors ocurr
}
}
Have you tried clearing the error console? I just copied this script into Unity and didn't get any errors or warnings. If you have, maybe consider deleting your Library
and Temp
folders?
Agreed. The code shown above does not seem to be the code that is causing that error.
Answer by Tom507 · Dec 15, 2017 at 12:21 PM
thanks for your suggestions but it turns out restarting my pc fixed it XD that was a really weird bug...
so it was propably as tcjulian and Bonfire-Boy suggested something that somehow stayed in cash from an earlyer compilation
so thanks again
Answer by KittenSnipes · Dec 13, 2017 at 12:23 AM
Why dont you try moving these two lines:
float moveVert = Input.GetAxis ("Vertical");
float moveHor = Input.GetAxis ("Horizontal");
Move them under the if statement in FixedUpdate. So it should look like this now:
void FixedUpdate (){
XYmovement = new Vector2 (rb.velocity.x, rb.velocity.z);
if( XYmovement.magnitude > maxSpeed)// clamping speed to max speed
{
XYmovement = XYmovement.normalized * maxSpeed;
rb.velocity = new Vector3 (XYmovement.x, rb.velocity.y, XYmovement.y);
}
float moveVert = Input.GetAxis ("Vertical");
float moveHor = Input.GetAxis ("Horizontal");
movement = new Vector3 (moveVert, Jump, -moveHor);
movement = transform.TransformDirection (movement);
rb.AddForce (movement*Accellaration);
// Taking controller mouse input
var Xin = Input.GetAxis ("Mouse X");
var Yin = Input.GetAxis ("Mouse Y");
LookAngleX += Xin * MouseSpeed;
LookAngleY += Yin * MouseSpeed;
LookAngleY = Mathf.Clamp (LookAngleY, -25f, 89f);
Camera.transform.localEulerAngles = new Vector3(-LookAngleY, Zoffset, 0f);
transform.eulerAngles = new Vector3(0f, LookAngleX, 0f);
//transform.eulerAngles = new Vector3(0f, LookAngleX, 0f);
Jump = 0f;
}
That doesn't change the scope of those variables (well, apart from making them unavailable to some lines that aren't using them), so why do you think it would help?
Just because of how weird if statements work. I think that it is a possibility but I was just throwing it out there. It is quite a weird error. I just moved the called variables closer to the lines of code that use them. I just wanted to at least throw out a suggestion rather than leave the question unanswered.