【发布时间】:2014-12-12 13:14:26
【问题描述】:
我是编程新手,我需要帮助来解决出现的问题。 我试图在另一个脚本中访问一个数组,通常没有问题。但我的情况是这样的:
例如:
脚本 A 包含几个具有不同非数字名称的数组。
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ScriptA : MonoBehaviour {
public int[] keys = new int[4];
public int[] screws= new int[4];
public int[] nails= new int[4];
public int[] iron= new int[4];
....
}
现在在脚本 B 中,我需要访问这些数组,但需要使用一个变量。我该怎么做?
例如:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ScriptB: MonoBehaviour {
ScriptA scriptA;
public string arrayName; // Here user could type in the name of an array
void Start()
{
scriptA = GameObject.Find("Object").GetComponent<ScriptA>();
// This try to access the array of course not works. But how?
scriptA.arrayname;
}
感谢您的想法和解决方案。我认为这不是一个难题,但我不知道如何解决它。
【问题讨论】:
-
如果您想访问例如
keys,您可以使用scriptA.keys或单个元素scriptA.keys[0]。 -
是的,我知道,但我需要使用 ScriptB 中的字符串变量“arrayName”访问数组
标签: c# arrays variables unity3d