Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Warning from variable never used?

Discussion in 'Scripting' started by Xhitman, Oct 20, 2018.

Thread Status:
Not open for further replies.
  1. Xhitman

    Xhitman

    Joined:
    Oct 30, 2015
    Posts:
    452
    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.
     
  2. Miggi124

    Miggi124

    Joined:
    Jul 31, 2017
    Posts:
    69
    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
     
    Bunny83 likes this.
  3. APSchmidtOfOld

    APSchmidtOfOld

    Joined:
    Aug 8, 2016
    Posts:
    4,473
    Remove them and see if your script still works. If it does, then these variables were really not used.
     
    Bunny83 likes this.
  4. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    And show your code
     
    Bunny83 likes this.
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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.
     
    Bunny83, DragonCoder and orb like this.
  6. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,033
    What Joe said - there's a reason modern compilers have a setting to consider unused code and variables errors.
     
    Joe-Censored likes this.
  7. Homeschoolunity

    Homeschoolunity

    Joined:
    Jul 30, 2022
    Posts:
    15
    Hi. I'm having some similar problems. Pls help.
     
  8. Homeschoolunity

    Homeschoolunity

    Joined:
    Jul 30, 2022
    Posts:
    15
    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<Rigidbody>().useGravity = false;
    GetComponent<Rigidbody>().velocity = Vector3.zero;
    GetComponent<Rigidbody>().freezeRotation = true;
    GetComponent<BoxCollider>().enabled = false;
    }

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

    Homeschoolunity

    Joined:
    Jul 30, 2022
    Posts:
    15
  10. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,459
    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.
     
  11. SurreyMuso

    SurreyMuso

    Joined:
    Mar 16, 2020
    Posts:
    62
    @DragonCoder positionSet is used in OnMouseDown... Mind you, it's nearly 5 minutes since I last made a mistake so am feeling very smug :)
     
  12. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,524
    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 :)
     
  13. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,967
    necro, off topic.
     
    Bunny83 likes this.
Thread Status:
Not open for further replies.