【问题标题】:How to connect my collectable items with the random generator and use a list of words for generating in UNITY 2D如何将我的收藏品与随机生成器连接并使用单词列表在 UNITY 2D 中生成
【发布时间】:2020-12-18 00:35:12
【问题描述】:

我有一个随机词生成脚本:

...

public TextMeshPro largeText;

public void BtnAction()
{
    CreateRandomString();
}

private void CreateRandomString(int stringLength = 10)
{
    int _stringLength = stringLength - 1;
    string randomString = "";
    string[] characters = new string[] { "A", "B", "C", "D" };
    for (int i = 0; i<= _stringLength; i++)
    {
        randomString = randomString + characters[Random.Range(0, characters.Length)];
    }
    largeText.text = randomString;
}

... 在我的游戏中,玩家必须收集我用收藏品标签制作的信件,以便在库存系统中用于收集。

...

private void OnTriggerEnter2D(Collider2D other) {

    if(other.CompareTag("Collectable"))
    {
        string inventoryType = other.gameObject.GetComponent<Collectable>().inventoryType;
        print("We have collected a:"+ inventoryType);

        inventory.Add(inventoryType);
        print("Inventory length:" + inventory.Count);
        Destroy(other.gameObject);

        string wordType = other.gameObject.GetComponent<Collectable>().inventoryType;
    }
}

...

我想代替:

string[] characters = new string[] { "A", "B", "C", "D" };

来自收集的字母的字符串。

之后,从现有的单词列表中生成一个单词。

我该怎么做?

【问题讨论】:

  • 这是你想要的吗,收集一些带有字母标签的东西......然后从每个字母中随机生成一个凝视?

标签: list unity3d connect collect generate


【解决方案1】:

在您的 OnTriggerEnter 脚本中,您可以将字母存储在这样的列表中,

//Instantiate your List
public List<string> mylist = new List<string>();

//Add the letters to your List 
mylist.Add(other.gameObject.GetComponent<Collectable>().inventoryType);

然后你必须参考你的另一个生成随机字符串的函数(我假设你知道如何做到这一点,我不能给你一个具体的方法,因为我不知道你这边的安排)。如果这两个函数都在一个脚本中,那就很容易了。

如果函数位于两个不同的脚本中,则必须将第一个脚本的 GameObject 引用到检查器中的第二个脚本,然后

public GameObject GameObject_with_your_first_Script;
......


firstScriptClassName myScript = GameObject_with_your_first_Script.GetComponent<fistScriptClassName>();

List referencedList = myScript.mylist;

希望这会有所帮助。我已经提到了完成所需工作所需的基本要素。如果有任何不清楚的地方,请告诉我。

【讨论】:

  • 谢谢!这很有帮助。 :)
猜你喜欢
  • 1970-01-01
  • 2016-08-01
  • 2013-12-17
  • 2017-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-30
  • 2011-06-24
相关资源
最近更新 更多