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

Trying to activate scripts without instantiating them as objects

Discussion in 'Scripting' started by Cedrus97, Jul 11, 2020.

  1. Cedrus97

    Cedrus97

    Joined:
    Jul 12, 2019
    Posts:
    18
    I want to use scripts that contain values and functions without instantiating them. I have a class and classes derived from that class. I want another script to go over a list which will contain the derived classes, but I don't know how to do so without making each one into an object, which I don't want to do both because that would add a lot of work and in just seems unnecessary.

    Thanks in advance for any suggestions and advises.
     
  2. dukerustfield

    dukerustfield

    Joined:
    Dec 5, 2019
    Posts:
    33
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    Fairly unclear what you're trying to do. Several options that might be helpful, depending:

    If you don't want your script to be attached to a Unity GameObject, you can just define a plain old C# class (i.e. don't inherit from MonoBehaviour).

    The words "object" and "instantiate" are also used to describe things you do with plain C# classes, but they don't mean quite the same thing that they typically do in a Unity context.

    If you want to avoid instantiating anything even in the C# sense, you can use static classes or static members.

    Also note that Unity has another magical type called ScriptableObject, which is kind of similar to MonoBehaviour except that it gets treated as an asset instead of an object in your scene.
     
  4. Cedrus97

    Cedrus97

    Joined:
    Jul 12, 2019
    Posts:
    18
    Thank you for the explanation