错误 CS0029:无法将类型“UnityEngine.UI.Slider[]”隐式转换为“UnityEngine.UI.Slider”
Error CS0029: Cannot implicitly convert type `UnityEngine.UI.Slider[]' to `UnityEngine.UI.Slider'
我在向唱首歌游戏添加滑块以告知您还剩多少来购买商品时出现错误 CS0029
。我正在关注 YouTube 视频以学习编程和 Unity。
Assets/scripts/UpgradeManger.cs(20,17): error CS0029: Cannot implicitly convert type UnityEngine.UI.Slider[]' to
UnityEngine.UI.Slider'
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class UpgradeManger : MonoBehaviour {
public Click click;
public UnityEngine.UI.Text itemInfo;
public float cost;
public float count = 0;
public int clickPower;
public string itemName;
private float basedCost;
public Color standard;
public Color afforable;
private Slider _slider;
void start () {
basedCost = cost;
_slider = GetComponentsInChildren<Slider> ();
}
void Update (){
itemInfo.text = itemName + " (" + count + ")" + "\nCost: " + cost + "/nPower: " + clickPower;
_slider.value = click.gold / cost * 100;
}
public void PurchasedUpgrade() {
if (click.gold >= cost) {
click.gold -= cost;
count += 1;
click.goldperclick += clickPower;
cost = Mathf.Round (cost * 1.15f);
basedCost = Mathf.Pow (cost, basedCost = cost);
}
}
}
[00:41] 您使用的是 GetComponentsInChildren 而不是 GetComponentInChildren
[00:41] GetComponentsInChildren returns 数组
我在向唱首歌游戏添加滑块以告知您还剩多少来购买商品时出现错误 CS0029
。我正在关注 YouTube 视频以学习编程和 Unity。
Assets/scripts/UpgradeManger.cs(20,17): error CS0029: Cannot implicitly convert type
UnityEngine.UI.Slider[]' to
UnityEngine.UI.Slider'
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class UpgradeManger : MonoBehaviour {
public Click click;
public UnityEngine.UI.Text itemInfo;
public float cost;
public float count = 0;
public int clickPower;
public string itemName;
private float basedCost;
public Color standard;
public Color afforable;
private Slider _slider;
void start () {
basedCost = cost;
_slider = GetComponentsInChildren<Slider> ();
}
void Update (){
itemInfo.text = itemName + " (" + count + ")" + "\nCost: " + cost + "/nPower: " + clickPower;
_slider.value = click.gold / cost * 100;
}
public void PurchasedUpgrade() {
if (click.gold >= cost) {
click.gold -= cost;
count += 1;
click.goldperclick += clickPower;
cost = Mathf.Round (cost * 1.15f);
basedCost = Mathf.Pow (cost, basedCost = cost);
}
}
}
[00:41] 您使用的是 GetComponentsInChildren 而不是 GetComponentInChildren [00:41] GetComponentsInChildren returns 数组