简体   繁体   English

Unity3D C#/变量已分配但从未使用过(?)

[英]Unity3D C#/Variable assigned but never used(?)

I have a little problem, I have my variables written and I can see them declared and used, but the unity console says its value is never used... The variable is distanceY 我有一个小问题,我写了我的变量,可以看到它们已声明和使用,但是统一控制台说它的值从未使用过。变量是distanceY

"Assets/Scripts/CameraWindowMove.cs(17,22): warning CS0414: The private field `CameraWindowMove.distanceY' is assigned but its value is never used" “资产/脚本/CameraWindowMove.cs(17,22):警告CS0414:分配了私有字段“ CameraWindowMove.distanceY”,但从未使用过它的值”

I don't know what's the problem, can some one help me? 我不知道出了什么问题,有人可以帮我吗?

Here's the sample of the code: 这是代码示例:

float distanceX, distanceY;

void Awake ()
{
    playerBoxCollider = GameObject.Find ("Player").GetComponent<BoxCollider2D> ();
    windowBoxCollider = GetComponent<BoxCollider2D> ();
    player = GameObject.Find ("Player");

}

void Start ()
{
    x = transform.position.x;
    y = transform.position.y;
    lookAhead = 0;
    distanceX = ((windowBoxCollider.bounds.size.x / 2) - (playerBoxCollider.bounds.size.x / 2));
    distanceY = ((windowBoxCollider.bounds.size.y / 2) - (playerBoxCollider.bounds.size.y / 2));

此警告告诉您,您仅将value设置为distanceY,但未在任何地方使用它的值。

This is just a build warning, it will not affect the functionality of your program. 这只是一个构建警告,不会影响程序的功能。 This is a valuable warning though, it is letting you know that you created a variable, assigned a value to it, but then never used it at a later point in your program (as far as static analysis can see). 但是,这是一个很有价值的警告,它使您知道已创建了一个变量,并为其分配了一个值,但是在以后的程序中再也没有使用过它(就静态分析而言)。

In C# you can disable them via a pragma at the top of your script file 在C#中,您可以通过脚本文件顶部的编译指示禁用它们

Some common are: 一些常见的是:

#pragma warning disable 0168  // variable declared but not used.
#pragma warning disable 0219  // variable assigned but not used.
#pragma warning disable 0414  // private field assigned but not used.

I would not suggest using these disables though as these warnings are useful. 我不建议使用这些禁用功能,因为这些警告很有用。 This is just for development. 这只是为了发展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM