Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How to make instances of a class attached to a game object?

Discussion in 'Scripting' started by woodkir000, Apr 7, 2022.

  1. woodkir000

    woodkir000

    Joined:
    Jan 13, 2022
    Posts:
    18
    I'm trying to make an array of instances of a class that is attached to one of my game objects, but I've run into a conundrum where it seems like I can't because according to one of the warnings, I can't make a "new" anything that extends MonoBehavior, but I'm under the impression that I can't instantiate and move (with transform component) game objects with a script if it doesn't extend MonoBehavior.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,150
    The general way of creating a new instance of a class that extends MonoBehavior is to either...
    Use AddComponent to add the script to an existing object.
    Instantiate a gameobject of a prefab that already has the script attached to it.

    If the script does not extend MonoBehavior, then it can't be attached to a gameobject, thus you can't get the transform through it since it wouldn't have one.

    Now, if you already have say, 10 enemies in your scene all with an enemy script on them, they would each have their own instance of the enemy class and you can add them to an array through the inspector or, use GetComponent to get the enemy script from them.

    Note, I would suggest some Unity basic tutorials to understand how components work. As that will help you understand much of what I just typed here.
     
    Kurt-Dekker likes this.
  3. woodkir000

    woodkir000

    Joined:
    Jan 13, 2022
    Posts:
    18
    Thanks for the reply, the main thing I was struggling with is that when trying to create an array of instances of each team (it's going to be PvP, same system on each team) and make a new instance; it said something about MonoBehavior not accepting the "new" keyword

    EDIT:
    "You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all"

    The exact line of code: t1sprites\[i\] = new PlayerSprite();
    (I used a for loop to loop through each part of the array of the team)
     
    Last edited: Apr 8, 2022
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,150
    I'm not sure if you are still asking a question, but you can't use new with classes that inherit from MonoBehaviour. The message tells you exactly that.

    Chances are, you want the script to already be on your player and if you want an array of them, then when you instantiate your players, just use GetComponent to grab the PlayerSprite script off of them.