Reputation: 47
I am currently doing a little 2D children_game project in Unity where the idea is for the character to collect letters on the map, and at the end of the level, the collected letters to form a random word. I was thinking that this might be done through a text document with words, from which the letters to generate the word at the end. I am not quite sure if this is how it's going to happen or there is a different option for this.
Do you have an idea of how this can happen and how can be generated the word with the possible largest amount of letters (one of the longest possible words in the existing ones), because after a few levels the players will have to create a meaningful sentence, which will give them points?
I have made the letters with tag 'collectable' :
private void OnTriggerEnter2D(Collider2D other) { ...
if(other.CompareTag("Collectable")) { string inventoryType = other.gameObject.GetComponent().inventoryType; print("We have collected a:"+ inventoryType);
inventory.Add(inventoryType);
print("Inventory length:" + inventory.Count);
Destroy(other.gameObject);
}
}
and I have made a script for random word generator with given letter:
public class Generate : MonoBehaviour {
private static void Swap(ref char a, ref char b)
{
if (a == b) return;
var temp = a;
a = b;
b = temp;
}
public static void GetPer(char[] list)
{
int x = list.Length - 1;
GetPer(list, 0, x);
}
private static void GetPer(char[] list, int k, int m)
{
if (k == m)
{
print(list);
}
else
for (int i = k; i <= m; i++)
{
Swap(ref list[k], ref list[i]);
GetPer(list, k + 1, m);
Swap(ref list[k], ref list[i]);
}
}
static void Main()
{
string str = "sagiv";
char[] arr = str.ToCharArray();
GetPer(arr);
}
}
The problem is that I don't know how to connect the collectable letters in the game with that script and show them in the end/victory screen as a list/a generated text. Can anyone help with this?
Upvotes: 1
Views: 568
Reputation: 1240
For each gameObject with the letter you can add a Tag with the letter such as: 'a' for the A letter and 'h' for the H and so on.
Then in the script of the Player you can add a: void OnTriggerEnter2D(Collision2D other) and check inside it every tag with if(other.compareTag("a")) and do it for each letter. If the compare tag returns true you can add the letter in a Char letterscollected[] or in a List<> and than randomly pick them with a simple for with random.range or anything else.
Upvotes: 1