Edit: я случайно сделал 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. Я проверил другие переменные, но этого не произошло.