- Home /
Private Field is assigned but its value is never used, but i use it
Hello everybody, i'm working on a 2D Platformer for Android and i've got a problem with my code it says i dont have the moveVelocity assigned but in my eyes i'm using the field later in my code. I don't know where the problem is.
I hope you could help me out.
I get this Error :
Assets/Scripts/PlayerController.cs(8,16): warning CS0414: The private field`PlayerController.moveVelocity' is assigned but its value is never used
Here is my Code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
public float moveSpeed;
private float moveVelocity;
private Rigidbody2D myRiBo;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
public bool isGrounded;
private Animator myAnim;
public Vector3 respawnPoint;
public LevelManager theLevelManager;
// Use this for initialization
void Start () {
myRiBo = GetComponent<Rigidbody2D> ();
myAnim = GetComponent<Animator> ();
respawnPoint = transform.position;
theLevelManager = FindObjectOfType<LevelManager> ();
}
// Update is called once per frame
void Update () {
// Ground Check for the Player
isGrounded = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, whatIsGround);
Move (Input.GetAxisRaw ("Horizontal"));
myAnim.SetFloat ("Speed", Mathf.Abs (myRiBo.velocity.x));
myAnim.SetBool ("Grounded", isGrounded);
// Check wich direction the Player should look
if (myRiBo.velocity.x > 0) {
transform.localScale = new Vector3 (1f, 1f, 1f);
} else if (myRiBo.velocity.x < 0) {
transform.localScale = new Vector3 (-1f, 1f, 1f);
}
}
public void Move (float moveInput) {
moveVelocity = moveSpeed * moveInput;
}
// If player is Grounded and want to Jump then Change Gravity
public void Jump () {
if (isGrounded) {
GetComponent<Rigidbody2D>().gravityScale *= -1;
transform.Rotate(0, 180, 180);
}
}
}
You can safely ignore this "Warning" (ie not an error). You do assign it but never actually use it. This is just Unity trying to help you to tie up loose ends and not have random variables that get forgotten about.
Answer by Hurri04 · Feb 04, 2018 at 10:39 PM
In the code you posted you only write to the variable but never actually read from it again. That's what the warning message tells you.
And since the variable is private no other class can access it so I'm assuming you didnt just forget to post some other code.
"but in my eyes i'm using the field later in my code"
if you mean that you still need to implement that then you should probably do that.
Hurri's answer is correct. You get this warning because a private field that you write to but never read from isn't actually doing anything; you're not actually using it.
$$anonymous$$ind you, it's not an error message; just a warning. If you're planning on writing code that uses the field later, feel free to ignore the warning; if not, you may want to remove that field to reduce clutter.
Yeah sorry i've got another script for the event triggers for the touch buttons.
With this two scripts my Character keeps running left and the left / right buttons don't work but the jump button works without any problems.
Here is the TouchControl script i'm using :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TouchControls : $$anonymous$$onoBehaviour {
private PlayerController thePlayer;
void Start () {
thePlayer = FindObjectOfType<PlayerController> ();
}
public void LeftArrow() {
thePlayer.$$anonymous$$ove (-1);
}
public void RightArrow() {
thePlayer.$$anonymous$$ove (1);
}
public void UnspressedArrow() {
thePlayer.$$anonymous$$ove (0);
}
public void Jump() {
thePlayer.Jump ();
}
}
this still does not read a value from the variable "moveVelocity", it just calls the method which writes the value.
use CTRL+F and search for "moveVelocity". you'll see that it only ever appears on the left side of "=".
to read from it it has to either appear on the right side (in case you have another calculation) or it has to be used as (part of) a parameter in e.g. transform.Translate.
If i understand this right if i put this line :
myRiBo.velocity = new Vector3 (moveVelocity, rigidbody2D.velocity.y, rigidbody2D.velocity.z);
Everything should be fine? Sorry if this questions sound dumb but i'm very new to scripting and coding and i'm trying to understand every mistake i made for future projects.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Encryption and Playerprefs (RSA) 1 Answer
Inconsistent jump height 1 Answer
Inconsistent jump height 1 Answer