-2

I am developing a videogame using Unity and I want the user to be able to choose from a group of characters who have different attributes and weapons.

Do I have to create a scene for every character for each level or there is a dynamic way of doing it?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Fábio Pires
  • 89
  • 1
  • 2
  • 10
  • I have trouble understanding why you would need several scenes actually. You have one selection scene, and several characters are there (either in 3D as proper GameObjects, or maybe selection is done only with a 2D GUI). In code, your characters are different instances of the same class or base class. The same scene can also be reused before each new level. And yes, you can have anything dynamic like the precise list of characters if this should change in between levels. – Pac0 Oct 21 '20 at 11:46
  • For example, the user can choose from Zelda(uses a sword) and Megaman(uses a gun). So the sprite I will be using will be different based on the character choosen by the user. So can I create a scene for Zelda ,duplicate it and change to Megaman? – Fábio Pires Oct 21 '20 at 11:48
  • ahhh I understand better, you were talking about the level scenes. No, hopefully and of course no, you don't need several scenes. You can and should have only one level scene, only the player object will be different, based on what was chosen before. Let me a couple of minutes just to search and show you how you can pass the information of character selection scene to your level scene. – Pac0 Oct 21 '20 at 11:51
  • thanks, I´ve been trying to find tutorials for this but I ain´t getting lucky – Fábio Pires Oct 21 '20 at 11:52
  • Basically, I'd suggest to go like this : you choose your actual character. Imagine you have a prefab Megaman and a prefab Zelda. Depending on what is chosen in the selection scene, you _instantiate_ the correct prefab, and you store this in a `public static GameObject player` field in some GameManager script. (this script is present on an empty object, in both scenes). In the level scene, in a `Start` method from a LevelManager, you just read the object `player` in theis field, and position it properly where it should be, and there you go. – Pac0 Oct 21 '20 at 11:59
  • You can have a look here for sharing data between scenes: https://stackoverflow.com/questions/32306704/how-to-pass-data-between-scenes-in-unity, there are other options than the `static` field. – Pac0 Oct 21 '20 at 12:00
  • So using a prefab also solves the problem of choosing between melee and long range combat – Fábio Pires Oct 21 '20 at 12:02
  • The key is have script that is able to handle both, either with inheritence, or with some parameters that you can have differerent on both prefabs. You have player script on both of prefab and the script will happily handle all different situations. – Pac0 Oct 21 '20 at 12:14
  • for example, I can have a parameter like character type and use an If condition ? – Fábio Pires Oct 21 '20 at 12:56
  • also, on the tutorials I´ve watched there is a different script for each action(example collision, movement) , should I do all of this in the same script? – Fábio Pires Oct 21 '20 at 12:57
  • It depends, often theree are several solutions. Some can be more or less clever or optimised, but it often depedns on context. The problem when you are a beginner, is that you can easily feel overwhelmed with all these choices. What I could suggest is : just try some way! At some point, you will "see" the consequences of your choice (often when they are bad :) ). I would be in favor of having multiple scripts for instance, but it seems a lot of work for nothing. But later, when you need to change a small things just for special case, you'r happy to have only one small file to change – Pac0 Oct 21 '20 at 13:02
  • "I can have a parameter like character type and use an If condition ?" Yes that's a possible solution. Personnally, I would not do it this way, because if you want to have more and more characters then you will have to add a `else if` everywhere for each character. But you don't "see" the problem until you hit it. – Pac0 Oct 21 '20 at 13:04

1 Answers1

2

Your scripts :

GameManager :

public class GameManager : MonoBehavior
{
    public static GameObject character;  // this field is to remember the selection. Since it is 'static', it will keep value in between scenes.
}

LevelManager :

public class LevelManager : MonoBehavior
{
    public void Start()
    {
         // read info stored in GameManager
         GameManager.character.transform.position = /* your start position */
    }
}

In scene 1 (selection):

  • store charcter selected in GameManager.character

In scene 2 (level):

  • An empty object with LevelManager script.

Note: Since we use static, I'm actually unsure that you even need the GameManager script to be attached to an empty object, for both scenes.

In the selection scne, just make sure to assign the proper thing to character (instance of Zelda or Megaman character prefab)

Pac0
  • 21,465
  • 8
  • 65
  • 74