C# 为什么变量在我给它赋值后会重置它的原始值?
编辑:我不小心在方法中使coor成为局部变量C# 为什么变量在我给它赋值后会重置它的原始值?,c#,unity3d,C#,Unity3d,编辑:我不小心在方法中使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;
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;
}
}
使用系统;
使用系统集合;
使用System.Collections.Generic;
使用System.Linq;
使用UnityEngine;
使用UnityEngine.UI;
公共班级建设:单一行为
{
[SerializeField]公共int所有者;
[SerializeField]公共int tpa、时间、级别;
[SerializeField]公用双maxHP、HP、atk、def、范围、产品、成本;
[SerializeField]专用向量2 coor;
[SerializeField]公共字符串类型,分支;
公共静态双总产品;
公共行动;
[SerializeField]bool buildStarted=false;
[SerializeField]bool buildCompleted=false;
int buildStartedOn;
bool complete=false;
公共静态列表myBuildings=新建列表();
公共向量2 Coor{get=>Coor;}
公共建筑(双倍生命,内转/动作,双倍攻击,双倍防御,双r,双专业,双c,内t,字符串类型,内lvl,字符串文胸,内自身,向量2 co)//constructor
{
拥有者=拥有者;
HP=HP;
tpa=每次动作的转弯次数;
atk=攻击;
def=防御;
范围=r;
prod=pro;
成本=c;
时间=t;
类型=典型;
水平=lvl;
分支=胸罩;
coor=co;
}
私人空间
{
//建筑=新建筑(HP、tpa、atk、def、范围、产品、成本、时间、类型、级别、分支机构、所有者、coor);
如果(所有者==0)
{
myBuildings.添加(此);
}
}
私有void更新()
{
如果(所有者==0)
{
如果(已完成)
{
如果(完成==错误)
{
totalprod+=产品;
如果(类型=“基本”)
{
//碱的tpa是适宜的
行动+=tpa;
}
调试日志(coor);
完整=正确;
}
}
else if(buildStarted==false)
{
如果(Currency.resource>=成本)
{
货币资源-=成本;
BuildDisplay.buildAction--;
Vector2 coor=NewBuilding.co;
调试日志(coor);
buildStarted=true;
buildStartedOn=Turn.turnCourt;
}
其他的
{
menumager.Messaging(“无资源”);
摧毁(游戏对象);
}
}
}
if(buildCompleted==false)
{
if(Turn.DeltaT(buildStartedOn)>=时间)
{
buildCompleted=true;
}
}
if(HP x.type==tType).ToList();
归还建筑物;
}
}
当我从检查员处查看时,coor总是0,0。
唯一正确的时间是在赋值后的Debug.Log()中。
这段代码只运行一次。
我把库尔列为私人。
我使用crtl+F发现这是我唯一一次更改coor的值。
我测试了其他变量,但没有出现这种情况。此处:
Vector2 coor = NewBuilding.co;
Debug.Log(coor);
coor
是您在Update
方法中的if(Currency.resource>=cost)
块(仅在其中可见)范围内声明的局部变量,它与私有向量2 coor无关代码>字段(您可以在Debug.Log(this.coor);
之后添加Debug.Log(coor);
来检查它,例如)。尝试将代码更改为:
coor = NewBuilding.co;
Debug.Log(coor);
它显示默认值,因为您没有为它指定任何值,因为您在方法中通过局部变量对它进行跟踪?