Warning from variable never used?

Assets/BodyPartFBX/WeaponEffectManager.cs(65,11): warning CS0414: The private field `WeaponEffectManager.RPGoHomeCounter’ is assigned but its value is never used

Why I get this warning? The variable is really in the switch() case.

I want to clear up the real useless variables, but such warning are everywhere.

Hello there!

This is a simple warning, reminding you that you’ve created a variable, but aren’t using it for anything (hence the “value is never used.”) If you’re not using this script/asset, I’d recommend deleting it to save space. If you need help with anything else, let me know.

Regards,

Miggi124

1 Like

Remove them and see if your script still works. If it does, then these variables were really not used.

1 Like

And show your code

1 Like

Generally you create a variable with the intention of using it. This is just telling you that you may have forgotten something, but it is not a code error in itself. It is pretty normal to get these warnings while you are developing your scripts, generally because you just haven’t gotten to the part in your development where you intend to use that variable. If you think you are finished writing that script and still see warnings like this, it is probably worth taking a look and thinking about why you put that variable in and why you chose not to use it.

3 Likes

What Joe said - there’s a reason modern compilers have a setting to consider unused code and variables errors.

1 Like

Hi. I’m having some similar problems. Pls help.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Pickup : MonoBehaviour
{
public Transform pickupPoint;

private Transform originalPoint;

private bool positionSet;
private bool pickedUp;

// Start is called before the first frame update
void Start()
{
originalPoint = pickupPoint;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}

// Update is called once per frame
void Update()
{
pickupPoint.transform.Translate(Camera.main.transform.forward * Time.deltaTime * 300 * Input.GetAxis(“Mouse ScrollWheel”), Space.World);
}

void OnMouseDown()
{
pickedUp = true;
if(!positionSet)
{
positionSet = true;
pickupPoint.position = transform.position;
}

transform.parent = pickupPoint.transform;
GetComponent().useGravity = false;
GetComponent().velocity = Vector3.zero;
GetComponent().freezeRotation = true;
GetComponent().enabled = false;
}

void OnMouseUp()
{
pickedUp = false;
positionSet = false;
pickupPoint.position = originalPoint.position;
transform.parent = null;
GetComponent().useGravity = true;
GetComponent().freezeRotation = false;
GetComponent().enabled = true;
}
}
[/code]

pickedUp

The error is admittedly sometimes unclear - “variable is not used” should better be “value is never used”. That’s the issue in your case.
You assign values to “positionSet” mutiple times but you never read from it. So effectively the variable is “unused” because its contents aka values are never used.

@DragonCoder positionSet is used in OnMouseDown… Mind you, it’s nearly 5 minutes since I last made a mistake so am feeling very smug :slight_smile:

Well, the variable “pickedUp” is never used anywhere. You only assign values to it but you never read that value anywhere. So it serves no purpose at all. That’s what the compiler has figured out. If can do this because the variable is private, so no other outside script or class could potentially access the value. So what’s the point of having it and assigning values to it?

In the end it’s just a warning, not an error. So the compiler tells you that this has no purpose and probably should be removed. Of course, maybe you need that state somewhere in the future, which is fine. However until you actually using the value the compiler will throw that warning against you.

ps: Please do not hijack other people’s posts, especially when they are 4+ years old. Create your own thread and when posting code, use code tags. There are two sticky posts in the scripting forum: Unity community code of conduct and one explaining how to use code tags properly. Please read them before posting. You already made 12 posts here on the forum and each of them was a necro post to hijack an old post, so it’s time to learn the rules :slight_smile:

necro, off topic.

1 Like