[英]UNity3D error displaying variable from another script c#
I got a UI Text element (inside a canvas) that I need to grab a value from another script. 我有一个UI Text元素(在画布内),我需要从另一个脚本中获取值。
The way I'm doing right now (and that was the closest I got to get it working) is: 我现在的工作方式(那是我要使其工作最接近的方式):
public class AttributeValueController : MonoBehaviour {
//public AttributeName attribute;
[SerializeField]
private Text attributeValue = null;
// Use this for initialization
void Start () {
attributeValue.text = CharacterGenerator._toon.GetPrimaryAttribute("Might").AdjustedBaseValue.ToString();
}
I got the text component I want to be changed set on on the inspector to the "attributeValue". 我在检查器上将要更改的文本组件设置为“ attributeValue”。
When I run this I get the errors 当我运行这个我得到错误
Assets/Scripts/HUD Classes/AttributeValueController.cs(19,58): error CS0120: An object reference is required to access non-static member `CharacterGenerator._toon'
资产/脚本/ HUD类/AttributeValueController.cs(19,58):错误CS0120:访问非静态成员`CharacterGenerator._toon'需要对象引用
and 和
NullReferenceException: Object reference not set to an instance of an object UnityEngine.UI.Graphic.OnRebuildRequested () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Graphic.cs:480) UnityEngine.UI.GraphicRebuildTracker.OnRebuildRequested () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/GraphicRebuildTracker.cs:33) UnityEngine.CanvasRenderer.RequestRefresh () (at C:/buildslave/unity/build/artifacts/generated/common/modules/UI/CanvasRendererBindings.gen.cs:314)
NullReferenceException:对象引用未设置为对象UnityEngine.UI.Graphic.OnRebuildRequested()的实例(位于C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/UI/Core/Graphic.cs:480) UnityEngine.UI.GraphicRebuildTracker.OnRebuildRequested()(在C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/GraphicRebuildTracker.cs:33)UnityEngine.CanvasRenderer.RequestRefresh()(在C:/ buildslave / unity / build / artifacts / generated / common / modules / UI / CanvasRendererBindings.gen.cs:314)
Anyone can help? 有人可以帮忙吗? When I replace this function by "0" it works... I've searched everywhere and I cannot fix it
当我将此功能替换为“ 0”时,它可以工作...我到处搜索,无法修复它
If CharacterGenerator
isn't a static class, you can't access it like that. 如果
CharacterGenerator
不是静态类,则不能这样访问它。 You would have to make an instance of the class to access properties. 您必须创建该类的实例才能访问属性。
CharacterGenerator cGen = new CharacterGenerator();
attributeValue.text = cGen._toon.GetPrimaryAttribute("Might").AdjustedBaseValue.ToString();
Although, given this is a new instance of the class, I would not expect these properties to be populated, but perhaps your class is structured so that they are. 尽管鉴于这是该类的新实例,但我不希望填充这些属性,但是也许您的类是这样构造的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.