c# - 为什么变量在我赋值后会重置其原始值?
问题描述
编辑:我不小心在方法中使 coor 成为局部变量。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
public class Building : MonoBehaviour
{
[SerializeField] public int owner;
[SerializeField] public int tpa, time, level;
[SerializeField] public double maxHP, HP, atk, def, range, prod, cost;
[SerializeField] private Vector2 coor;
[SerializeField] public string type, branch;
public static double totalprod;
public static int actions;
[SerializeField] bool buildStarted = false;
[SerializeField] bool buildCompleted = false;
int buildStartedOn;
bool complete = false;
public static List<Building> myBuildings = new List<Building>();
public Vector2 Coor { get => coor;}
public Building(double hp, int turn_per_action, double attack, double defence, double r, double pro, double c, int t, string typ, int lvl, string bra, int own, Vector2 co)//constructor
{
owner = own;
HP = hp;
tpa = turn_per_action;
atk = attack;
def = defence;
range = r;
prod = pro;
cost = c;
time = t;
type = typ;
level = lvl;
branch = bra;
coor = co;
}
private void Awake()
{
//Building building = new Building(HP, tpa, atk, def, range, prod, cost, time, type, level, branch, owner, coor);
if(owner == 0)
{
myBuildings.Add(this);
}
}
private void Update()
{
if(owner == 0)
{
if (buildCompleted)
{
if (complete == false)
{
totalprod += prod;
if (type == "Base")
{
//tpa of base is apt
actions += tpa;
}
Debug.Log(coor);
complete = true;
}
}
else if (buildStarted == false)
{
if (Currency.resource >= cost)
{
Currency.resource -= cost;
BuildDisplay.buildAction--;
Vector2 coor = NewBuilding.co;
Debug.Log(coor);
buildStarted = true;
buildStartedOn = Turn.turnCourt;
}
else
{
MenuManger.Messaging("no resource");
Destroy(gameObject);
}
}
}
if(buildCompleted == false)
{
if(Turn.DeltaT(buildStartedOn) >= time)
{
buildCompleted = true;
}
}
if(HP <= 0)
{
Destroy(gameObject);
}
}
public void OnBuilding()
{
if (buildCompleted)
{
if (SelectAction.isTargeting)
{
if (owner != 0)
{
Attacking.TargetBuilding = this;
Attacking.isAttacking = true;
}
else
{
MenuManger.Messaging("invalid target");
}
}
else if (owner == 0)
{
SelectAction.ActingBuilding = this;
MenuManger.ChangeMenu("Action", true);
}
}
else
{
MenuManger.Messaging("incomplete building");
}
}
public static List<Building> FindBuildingWithType(string tType)
{
var buildings = myBuildings.Where(x => x.type == tType).ToList();
return buildings;
}
}
当我从检查员那里查看 coor 时,它始终为 0,0。唯一正确的时间是在分配值之后的 Debug.Log() 中。那段代码只运行一次。我将 coor 设为私有。我用crtl+F发现这是我唯一一次改变coor的值。我测试了其他变量,但没有发生这种情况。
解决方案
这里:
Vector2 coor = NewBuilding.co;
Debug.Log(coor);
coor
是您在方法if (Currency.resource >= cost)
中在块的范围内声明的局部变量(并且仅在其中可见),Update
它与字段无关(例如,private Vector2 coor;
您可以检查它是否添加)。尝试将代码更改为:Debug.Log(this.coor);
Debug.Log(coor);
coor = NewBuilding.co;
Debug.Log(coor);
推荐阅读
- puppeteer - 我怎么可能刮掉没有定义名称的页面并在元素上使用随机类名?
- angular - 根据所需条件使用不同划分的问题
- r - 创建新的 R 包时如何正确组织小插图和 inst 文件夹
- python-3.x - 为什么列表中的输入会被覆盖
- javascript - 减少道具数量
- javascript - 从AngularJS中的对象访问所有嵌套值的最快方法
- sapui5 - SAPUI5:视图/页面上的按键
- android - Xamarin.Forms:如何使用 DPI 和权重
- pyspark - 使用 ANSI 编码读取 pyspark 中的 CSV 文件
- google-cloud-platform - 如何在 GCP 中的私有 GKE 上启用 NAT?