0

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.

  1. Tuple
  2. Set
  3. Array
  4. List
  5. Dictionary
  6. Stack
  7. Queue
  8. 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.

4
  • One seems to wind up with "Dictionaries of dictionaries" during game play; in order to handle one to many; and many to many realtionships; that come into being and then vanish. One can be attacked by many attackers. One can fire on many enemies ("troops behind"). These all come into play when considering "options": Who's attacking (me); who am I inflicting losses on; etc. GameObject has a unique id; that's where it starts. Commented Jul 27, 2024 at 19:12
  • If so, how can I use ID when considering those options? Commented Jul 27, 2024 at 19:43
  • If the objects are not modified and remain until the end of the game, why do you need to track them? Can you give some examples of what you are attempting, because as it stands this is extremely vague. Commented Jul 31, 2024 at 19:54
  • 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. Commented Aug 3, 2024 at 8:45

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.