[英]Unity Scripting missing definition for a variable and a unassigned local variable c#
I am fairly new to unity and game scripting and I am having problems starting out. 我对团结和游戏脚本还很陌生,开始时遇到问题。
This is my playstate.cs (I am only pasting the relevant code lines) 这是我的playstate.cs(我只粘贴相关的代码行)
using UnityEngine;
using Assets.Code.Interfaces;
using Assets.Code.Scripts;
using System.Collections; // dicionario
using System.Collections.Generic; // dicionario
namespace Assets.Code.States
gametime = (int)Time.timeSinceLevelLoad / 5;
GUI.Box (new Rect (Screen.width - 650, 10, 100, 25), gametime.ToString() ); // GAME TIME HOURS
float test;
if (LoadDiagram.diagramaCarga.TryGetValue(gametime, out test)) // Returns true.
{
GUI.Box (new Rect (Screen.width - 650, 275, 50, 25), test.ToString ());
}
And this is where my LoadDiagram is stored: 这是我的LoadDiagram的存储位置:
using UnityEngine;
using Assets.Code.Interfaces;
using System.Collections; // dicionario
using System.Collections.Generic; // dicionario
using System;
namespace Assets.Code.Scripts
{
public class LoadDiagram
{
public LoadDiagram ()
{
Dictionary<int, float> diagramaCarga = new Dictionary<int, float>();
diagramaCarga.Add(0, 4.2F);
diagramaCarga.Add(1, 4F);
diagramaCarga.Add(2, 3.6F);
diagramaCarga.Add(3, 3.4F);
diagramaCarga.Add(4, 3.2F);
diagramaCarga.Add(5, 3F);
}
}
}
So, I have two errors: 因此,我有两个错误:
error CS0117: Assets.Code.Scripts.LoadDiagram' does not contain a definition for
diagramaCarga' 错误CS0117:
Assets.Code.Scripts.LoadDiagram' does not contain a definition for
error Assets/Code/States/PlayState.cs(112,87): error CS0165: Use of unassigned local variable `test' 错误Assets / Code / States / PlayState.cs(112,87):错误CS0165:使用未分配的局部变量“ test”
Have any idea of what is going on? 有什么想法吗? Thanks in advance!
提前致谢!
Well, @cubrr's comment is correct, but he didn't put it as an answer. 好吧,@ cubrr的评论是正确的,但他没有将其作为答案。
diagramaCarga
only exists inside the local scope (inside the curly braces) of theLoadDiagram()
constructor method.diagramaCarga
仅存在于LoadDiagram()
构造函数方法的局部范围内(大括号内)。 You need create a public property or field for it inside the class scope.您需要在类范围内为其创建一个公共属性或字段。
More specifically, you are trying to access it as a static field inside your other class, which means you would need the LoadDiagram
class to look like this: 更具体地说,您尝试将其作为其他类内的静态字段进行访问,这意味着您将需要
LoadDiagram
类如下所示:
public class LoadDiagram
{
public static Dictionary<int, float> diagramaCarga = new Dictionary<int, float>();
// this is a "static block" which acts like a constructor for static objects,
// as static classes do not use constructors.
// If I got the syntax correct, I've never actually used one of these.
static LoadDiagram(){ // !!edited this line!!
diagramaCarga.Add(0, 4.2F);
diagramaCarga.Add(1, 4F);
diagramaCarga.Add(2, 3.6F);
diagramaCarga.Add(3, 3.4F);
diagramaCarga.Add(4, 3.2F);
diagramaCarga.Add(5, 3F);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.