I'm going to make a game with Unity.(I'm new to programming) Within each level of Unity, there are the borders, arrows, which are GameObjects. What is the most efficient C# coding for managing these? These are not modified and will remain until the end of the game.
This is what I want: When you press the right arrow of background 2, you cross the boundary and move the character to background 3, and when you press the left arrow, you move to background 1. Also, I want the character's conversation to be different depending on the current background. I also want to record how many times I visited this background to control my conversations.
I had a hard time managing each object by declaring this as a public variable all separately. I repeatedly pulled each object with a mouse. I attached this to the player's script.(PlayerManager) example:
public GameObject level1; public GameObject border1OfLevel1; public GameObject border2OfLevel1; public GameObject border3OfLevel1; public GameObject arrow1OfLevel1; public GameObject arrow2OfLevel1; public GameObject level2; //and so on So I asked Chat GPT to tell me various variable declaration methods(?) to solve this problem. These are the methods it's considered.
- Tuple
- Set
- Array
- List
- Dictionary
- Stack
- Queue
- LinkedList
I heard Chat Gpt's answer but I still don't understand the code it made. I don't have the ability to tell if this is efficient. Here is its code:
GameObject[][] borderObjects = new GameObject[backgrounds.GetLength(0)][]; for (int i = 0; i < backgrounds.GetLength(0); i++) { int numberOfBorders = backgrounds[i].transform.childCount; borderObjects[i] = new GameObject[numberOfBorders]; for (int j = 0; j < numberOfBorders; j++) { Transform borderTransform = backgrounds[i].transform.GetChild(j); borderObjects[i][j] = borderTransform.gameObject; } } I also tried to use Unity's Prefab function, but I couldn't use it because each object's functions were all different. If you're busy, I'd appreciate it if you could link me to a related site that can be helpful.