C# 跨2个脚本访问类数组中的多个变量
我有两个脚本。脚本A包含一个类的数组。该类包含两个变量,一个int和一个string。脚本B包含我正在执行的大部分代码,需要从脚本A中的类数组访问变量 (基本上,我正在尝试使用网格制作一个程序生成的平铺贴图。在第一步中,我制作了网格。在第二步中,我希望使用平铺类制作平铺,该类可以生成与平铺相关的旧值,[其名称、图形、属性等。]理论上,我会做第三遍来制作纹理,因此需要访问每个瓷砖中包含的图形信息。) 我的问题是,无论我以何种方式尝试和执行,我似乎都无法让脚本B将脚本A中的数组识别为已初始化。相反,我在尝试访问变量的行中得到错误“Object reference not set to a instance of a Object” 我尽我所能做到这一点,甚至使用了列表,但都无法实现。诚然,我对C#和Unity都是新手。我想知道为什么我所做的是错误的,以及如何修复它以更好地理解这个过程 如能提供任何帮助,我们将不胜感激。我的代码的相关部分如下: 脚本AC# 跨2个脚本访问类数组中的多个变量,c#,arrays,class,variables,unity3d,C#,Arrays,Class,Variables,Unity3d,我有两个脚本。脚本A包含一个类的数组。该类包含两个变量,一个int和一个string。脚本B包含我正在执行的大部分代码,需要从脚本A中的类数组访问变量 (基本上,我正在尝试使用网格制作一个程序生成的平铺贴图。在第一步中,我制作了网格。在第二步中,我希望使用平铺类制作平铺,该类可以生成与平铺相关的旧值,[其名称、图形、属性等。]理论上,我会做第三遍来制作纹理,因此需要访问每个瓷砖中包含的图形信息。) 我的问题是,无论我以何种方式尝试和执行,我似乎都无法让脚本B将脚本A中的数组识别为已初始化。相反,
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TileData {
[System.Serializable]
public class Tile
{
public int tileGraphic = 0;
public string tileName = "Unknown";
}
public Tile[] tileArray = new Tile[3];
public void makeArray()
{
tileArray[0] = new Tile { tileGraphic = 0, tileName = "Grassland" };
tileArray[1] = new Tile { tileGraphic = 1, tileName = "Water" };
tileArray[2] = new Tile { tileGraphic = 2, tileName = "Forest" };
tileArray[3] = new Tile { tileGraphic = 3, tileName = "Mountain" };
}
脚本B
public void BuildMap()
{
//There's other stuff above here that works, I have omitted it for brevity.
BuildTiles();
}
public TileData tileTypes;
void BuildTiles()
{
Debug.Log("Test Tile Graphic = " + tileTypes.tileArray[0]);
//Debug.Log("Test Tile Graphic = " + tileTypes.tileArray[0].tileGraphic);
}
**正如所承诺的那样,这两种股票的全部内容如下。我省略了注释,并注释掉了保持可读性的尝试。谢谢大家的帮助
脚本A
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[ExecuteInEditMode]
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(MeshCollider))]
public class missionMap : MonoBehaviour {
public int sizeX = 25;
public int sizeZ = 25;
public float tileSize = 1.0f;
public Texture2D testTile;
public int tileResolution;
void Start ()
{
BuildMap();
//public TileData tileTypes = new TileData();
}
//Build the Map
public void BuildMap()
{
int numTiles = sizeX * sizeZ;
int numTris = numTiles * 2;
int vertSizeX = sizeX + 1;
int vertSizeZ = sizeZ + 1;
int numVerts = vertSizeX * vertSizeZ;
Vector3[] vertices = new Vector3[numVerts];
Vector3[] normals = new Vector3[numVerts];
Vector2[] uv = new Vector2[numVerts];
int[] triangles = new int[numTris * 3];
int x, z; //Loop counters.
for (z = 0; z < vertSizeZ; z++)
{
for (x = 0; x < vertSizeX; x++)
{
vertices[z * vertSizeX + x] = new Vector3(x * tileSize, 0, z * tileSize); //Fills the vertices array with new vector 3's.
normals[z * vertSizeX + x] = Vector3.up;
uv[z * vertSizeX + x] = new Vector2((float)x / sizeX, (float)z / sizeZ);
}
}
for (z = 0; z < sizeZ; z++)
{
for (x = 0; x < sizeX; x++)
{
int squareIndex = z * sizeX + x;
int triOffset = squareIndex * 6; //We can offset it by 6 (number of verts in a tile [2 traingles * 3 points each])
triangles[triOffset + 0] = z * vertSizeX + x + 0; [NW corner of the square]
triangles[triOffset + 1] = z * vertSizeX + x + vertSizeX + 0;
triangles[triOffset + 2] = z * vertSizeX + x + vertSizeX + 1;
triangles[triOffset + 3] = z * vertSizeX + x + 0;
triangles[triOffset + 4] = z * vertSizeX + x + vertSizeX + 1;
triangles[triOffset + 5] = z * vertSizeX + x + 1;
}
}
Mesh mesh = new Mesh();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.normals = normals;
mesh.uv = uv;
MeshFilter meshFilter = GetComponent<MeshFilter>();
MeshRenderer meshRenderer = GetComponent<MeshRenderer>();
MeshCollider meshCollider = GetComponent<MeshCollider>();
meshFilter.mesh = mesh;
meshCollider.sharedMesh = mesh;
BuildTiles();
}
//Build the Tiles
public TileData tileTypes = new TileData();
void BuildTiles()
{
Debug.Log("Test Tile Graphic = " + tileTypes.tileArray[0].tileGraphic);
int[,] tilePosition;
tilePosition = new int[sizeX, sizeZ];
for (int z = 0; z < sizeZ; z++)
{
for (int x = 0; x < sizeX; x++)
{
tilePosition[x, z] = 0;
//Debug.Log("Tile Position: " + x + "," + z);
使用UnityEngine;
使用系统集合;
使用System.Collections.Generic;
[执行编辑模式]
[要求组件(类型(网状过滤器))]
[RequiredComponent(typeof(MeshRenderer))]
[所需组件(类型(网格对撞机))]
公共类任务地图:单一行为{
公共int sizeX=25;
公共int sizeZ=25;
公共浮点数tileSize=1.0f;
公共纹理2D测试瓷砖;
公共解决方案;
无效开始()
{
BuildMap();
//public TileData tileTypes=新的TileData();
}
//绘制地图
公共void BuildMap()
{
int numTiles=sizeX*sizeZ;
int numTris=numTiles*2;
int vertSizeX=sizeX+1;
int vertSizeZ=sizeZ+1;
int numVerts=vertSizeX*vertSizeZ;
Vector3[]顶点=新Vector3[numVerts];
向量3[]法线=新向量3[numVerts];
Vector2[]uv=新Vector2[numVerts];
int[]三角形=新int[numTris*3];
int x,z;//循环计数器。
对于(z=0;z using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TileData {
[System.Serializable]
public class Tile
{
public int tileGraphic = 0;
public string tileName = "Unknown";
}
public Tile[] tileArray = new Tile[4];
public void makeArray()
{
//for (int i = 0; 1 < tileArray.Length; i++)
//tileArray = new Tile[3];
tileArray[0] = new Tile { tileGraphic = 0, tileName = "Grassland" };
tileArray[1] = new Tile { tileGraphic = 1, tileName = "Water" };
tileArray[2] = new Tile { tileGraphic = 2, tileName = "Forest" };
tileArray[3] = new Tile { tileGraphic = 3, tileName = "Mountain" };
}
public void makeArray()
{
tileArray[0] = new Tile { tileGraphic = 0, tileName = "Grassland" };
tileArray[1] = new Tile { tileGraphic = 1, tileName = "Water" };
tileArray[2] = new Tile { tileGraphic = 2, tileName = "Forest" };
---**tileArray[3] = new Tile { tileGraphic = 3, tileName = "Mountain" };**---
}
void Start()
{
tileTypes.makeArray();
BuildMap();
//public TileData tileTypes = new TileData();
}
void BuildTiles()
{
tileTypes.makeArray();
Debug.Log("Test Tile Graphic = " + tileTypes.tileArray[0].tileGraphic);
}