已解决 - 似乎我试图引用一个未通过引用传递的结构(vector2)。因此代码是变量/列表没有相互反映。
谢谢大家。
这是我正在使用的代码。我希望能够更改List引用的变量,并使列表反映更新的变量,而无需搜索列表并每次更新它。这将使我更容易更新我的buff,而不是。
这可能吗?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Buff : MonoBehaviour{
// (Buff Amount, Timer)
Vector2 thrust = new Vector2(1,0);
Vector2 mass = new Vector2(1,0);
Vector2 shield = new Vector2(1,0);
Vector2 armor = new Vector2(1,0);
Vector2 structure = new Vector2(1,0);
Vector2 regen = new Vector2(1,0);
Vector2 explosionRadius = new Vector2(1,0);
Vector2 damage = new Vector2(1,0);
Vector2 disable = new Vector2(0,0);
List<Vector2> buffs = new List<Vector2>();
// Use this for initialization
void Start () {
buffs.Add(thrust);
buffs.Add(mass);
buffs.Add(shield);
buffs.Add(armor);
buffs.Add(structure);
buffs.Add(regen);
buffs.Add(explosionRadius);
buffs.Add(damage);
buffs.Add(disable);
}
// Update is called once per frame
void Update () {
// Makes Regen variable change.
regen[0] = 5;
// Can I make it affect what is in the List of Vector 2's?
foreach (Vector2 buff in buffs)
{
if (buff.magnitude > 1)
UpdateBuff(buff);
}
}
// Therefore this will be called. Then the variable outside can also be updated?
void UpdateBuff(Vector2 inBuff)
{
Debug.Log("Updating Buff: " + inBuff.ToString());
if ((inBuff[1] -= Time.deltaTime) <= 0)
{
inBuff[0] = 1;
inBuff[1] = 0;
}
}
答案 0 :(得分:0)
“Foreach”变量是只读的,这意味着你无法通过它们 参考
答案 1 :(得分:0)
修复了使用自定义类的问题。
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Buff : MonoBehaviour{
// (Buff Amount, Timer)
public BuffInfo thrust = new BuffInfo(1,0);
public BuffInfo mass = new BuffInfo(1,0);
public BuffInfo shield = new BuffInfo(1,0);
public BuffInfo armor = new BuffInfo(1,0);
public BuffInfo structure = new BuffInfo(1,0);
public BuffInfo regen = new BuffInfo(1,0);
public BuffInfo explosionRadius = new BuffInfo(1,0);
public BuffInfo damage = new BuffInfo(1,0);
public BuffInfo disable = new BuffInfo(0,0);
public List<BuffInfo> buffs = new List<BuffInfo>();
float deltaUpdates = 0;
// Use this for initialization
void Start () {
buffs.Add(thrust);
buffs.Add(mass);
buffs.Add(shield);
buffs.Add(armor);
buffs.Add(structure);
buffs.Add(regen);
buffs.Add(explosionRadius);
buffs.Add(damage);
buffs.Add(disable);
regen.power = 5;
}
// Update is called once per frame
void Update () {
// Makes Regen variable change.
if ((deltaUpdates += Time.deltaTime) < 1)
return;
// Can I make it affect what is in the List of Vector 2's?
foreach (BuffInfo buff in buffs)
{
if (buff.magnitude() > 1)
UpdateBuff(buff);
}
deltaUpdates = 0;
}
// Therefore this will be called. Then the variable outside can also be updated?
void UpdateBuff(BuffInfo inBuff)
{
Debug.Log("Updating Buff: " + inBuff.ToString());
if ((inBuff.power -= deltaUpdates) <= 0)
{
inBuff.power = 1;
inBuff.time = 0;
}
}
}