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. Dismiss Notice

Why list of normal class creates new object and classes in behaviour class not

Discussion in 'Scripting' started by newGuyInThat, Jun 19, 2016.

  1. newGuyInThat

    newGuyInThat

    Joined:
    Oct 21, 2015
    Posts:
    43
    when i wrote:

    using UnityEngine;
    using System.Collections;

    [System.Serializable]
    public class classs : MonoBehaviour {
    int x;
    }

    and list for classs, the another script is for ready elements and when i wrote:

    using UnityEngine;
    using System.Collections.Generic;

    public class UnitsManager : MonoBehaviour {
    public List<classs> CLASS = new List<classs>();

    [System.Serializable]
    public class Normalclasss
    {
    public int x;
    }
    }

    the list creates new elements.
    Why is that? What i can do to create new elements from class inheritable from Monobehaviour?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    First, use code tags.
    Second, you really should use more meaningful class names and variable names other than classs and CLASS.
    Third...I'm not sure what you're asking. If you make a list and initialize it the way you did, it's just an empty list, nothing else till you add stuff to it.
     
  3. newGuyInThat

    newGuyInThat

    Joined:
    Oct 21, 2015
    Posts:
    43
    Sorry for my english, i show you the problem in screens
    when i create list of class : Monobehaviour it looks in that way:
    https://zapodaj.net/9b2c461377356.png.html
    when i create list of class IN the class : Monobehaviour it looks that:
    https://zapodaj.net/111377d88a35f.png.html
    I want to create list of class : Monobehaviour so as to it look that class in the class : Monobehaviour. Do You understood? XD
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    It has nothing to do with nested/inner classes. You can also define the "normal" class outside and it will still look the same.

    The option to set the size simply allows to populate the list via the inspector, if necessary.
    That's what Unity does automatically for you when you use a normal class. If you don't need any entries, don't use that feature.

    In regards to MonoBehaviours it's a little different. You're meant to put existing instances there, which are attached to game objects and live in the scene already. Unity doesn't know which instances you want to put there, as you're the only one who knows which exact instance (of possibly hundreds or thousands) you want to throw into these slots.

    If Unity populated the list with MonoBehaviours for you, that would require to manually assign these behaviours to your gameobjects which may already be set up properly. Or Unity would need to create new gameobjects for each instance, which is not what you want in most cases.
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    Your first image is a list of classs. Which means it's a list where you can drop objects that contain the classs script on them.

    Your second image is a list of variables within a class. It's useful if you're trying to set up something like. Enemy spawn waves. You could have a class

    public string enemyName;
    public int waveNum;
    public int waveCount;

    For example, and by creating a list of this, you can fill in all those values. The reason this works nicely is you can fill in those values and keep it all together.

    The first happens because you have a list of a script that inherits from monobehavior. The second happens because you have a class that is Serializable, with public variables that doesn't inherit from monobehavior.

    If your first one is a list of enemies, you drag your enemies with the enemy script on them into the list. You set the public varaibles for those enemies on the enemy itself, modifying each separate script.

    The second really is just a way of doing it without having to have existing scripts, but where you just set values.