Search Unity

Instantiating a class: purpose & context?

Discussion in 'Scripting' started by Ian2333, May 30, 2019.

  1. Ian2333

    Ian2333

    Joined:
    Mar 10, 2018
    Posts:
    4
    Hi All,

    Beginner here. I have come across many tutorials that does something like: "Enemy slime = new Enemy(5, 20);" .

    I understand that the above script instantiates a pre-written Enemy class to be called slime. Slime will have let's say 5 damage and 20 health. Similarly, I can also instantiate skeletons or goblins with diff stats from the Enemy class.

    My question is, what's the purpose for instantiating Enemy to slime? In what context is slime used for? Maybe I want an actual slime to be spawned inside the actual game? But shouldn't it be represented as a gameobject that contains components like transform, mesh renderer and rigidbody? Do I just attach the instantiated slime to a instantiated gameobject like a component but with code?

    Sorry I'm a bit lost...
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    "slime" means overall nothing. It's a variable name. In this case it just represents an instance of the class Enemy. Since it's using "new" it's not a monobehaviour script and thus isn't attached to a gameobject, so it's simply data being used. In this case you could name the variable whatever you want, it was just named slime in this case.

    If you create a Gameobject with the name slime and create a prefab, you instantiate it to add it to the scene.

    The above code doesn't create the Gameobject.
     
  3. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,635
    In that case you probably don't need to use "new" at all. Just use GameObject.Instantiate:

    https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

    Instantiate is part of the Unity API. It's actually using "new" behind the scenes to create the instances.
     
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    As for what purpose you would need for instantiating an object, it just depends on what you need to do with it.

    Here's an example, say I'm going to make a very simple game where the player is prompted with a message stating a direction, and they must press an arrow key corresponding to the direction in order to advance. An incorrect input results in a game over.

    I could create a custom class meant for displaying the prompt and assigning a correct direction input that the player must match:
    Code (CSharp):
    1. public class PromptInput {
    2.    public string message;
    3.    public KeyCode correctDirection;
    4.  
    5.    //Constructor to initialize values when creating a new PromptInput object.
    6.    public PromptInput(string prompt, KeyCode correctDirection) {
    7.       this.message = message;
    8.       this.correctDirection= correctDirection;
    9.    }
    10. }
    I can then create an array of these objects in a MonoBehaviour script and iterate through them while reading the player's input to check if it matches with each PromptInput's "correctDirection" field:
    Code (CSharp):
    1. public class Example : MonoBehaviour {
    2.    public PromptInput[] promptInputs;
    3.    private int currentIndex;
    4.    private KeyCode[] arrowKeys;
    5.  
    6.    private bool gameOver = false;
    7.  
    8.    void Start() {
    9.       //Create an array of PromptInput objects to iterate through.
    10.       //Each PromptInput has its own message that will be logged into the console, and a KeyCode input that the player must match.
    11.       promptInputs = new PromptInput[] {
    12.          new PromptInput("UP", KeyCode.UpArrow),
    13.          new PromptInput("RIGHT", KeyCode.RightArrow),
    14.          new PromptInput("UP", KeyCode.UpArrow),
    15.          new PromptInput("DOWN", KeyCode.DownArrow),
    16.          new PromptInput("DOWN", KeyCode.DownArrow),
    17.          new PromptInput("LEFT", KeyCode.LeftArrow),
    18.          new PromptInput("RIGHT", KeyCode.RightArrow),
    19.          new PromptInput("LEFT", KeyCode.LeftArrow)
    20.       };
    21.  
    22.       //Grouping all the arrow key KeyCodes into an array for easy reference.
    23.       arrowKeys = new KeyCode[] {
    24.          KeyCode.UpArrow,
    25.          KeyCode.DownArrow,
    26.          KeyKode.LeftArrow,
    27.          KeyCode.RightArrow
    28.       };
    29.  
    30.       //Start by printing the message from the first PromptInput object in the array.
    31.       currentIndex = 0;
    32.       Debug.Log(promptInputs[currentIndex].message);
    33.    }
    34.  
    35.    void Update() {
    36.       //Disable this script when there is a game over.
    37.       if(gameOver) {
    38.          enabled = false;
    39.          return;
    40.       }
    41.  
    42.       //Check if the player has pressed any of the arrow keys, and if so, compare the key to the correct direction of the current PromptInput index in the array.
    43.       for(int i = 0; i < arrowKeys.Length, i++) {
    44.          KeyCode playerDirection = Input.GetKeyDown(arrowKeys[i]);
    45.    
    46.          if(playerDirection) {
    47.             CompareInput(playerDirection);
    48.          }
    49.       }
    50.    }
    51.  
    52.    private void CompareInput(KeyCode playerDirection) {
    53.       //If the arrow key that the player has pressed is the same KeyCode as the current PromptInput's correctDirection field,
    54.       //then the player can advance to the next iteration of the PromptInput array.
    55.       if(playerDirection == promptInputs[currentIndex].correctDirection) {
    56.          currentIndex++;
    57.          Debug.Log(promptInputs[currentIndex].message);
    58.       }
    59.  
    60.       //If the arrow key that the player has pressed is NOT the same KeyCode as the current PromptInput's correctDirection field,
    61.       //then end the game.
    62.       else {
    63.          gameOver = true;
    64.       }
    65.    }
    66. }
     
  5. Ian2333

    Ian2333

    Joined:
    Mar 10, 2018
    Posts:
    4
    Thanks for the replies guys! Guess I was trying to understand the custom C# class under unity's "gameobject-component/monobehaviour" context. They are apparently different things.