C# script as variable

Hi,
Can’t seem to find the answer, and I am surprised I am not able to find any answer about this.
The idea is simple : I would like to assign a c# script as a variable, and use it during runtime to do a AddComponent, to be able to do something like this :

[System.Serializable]
public class PowerUp
{
    public string name;
    public string description;
    public Sprite thumbnail;
    public Component script;
}

In this very specific scenario, a script manages a power up behaviour : adding it or removing it enables or disable the PowerUp. I suppose this is how The Binding Of Isaac manages the items ?

public void AddPowerUp (PowerUp p)
{
    transform.AddComponent<p>();
}

Right now I am using a prefab system wich works fine but is less intuitive to work with, and creates uncessearry gameObjects (while one gameObject could hoold all the created components).
My understanding is that Component can only be assigned for scripts in gameObject. I also undestand that a limitation can be public variables in the script I want to assign (which I dont have).

Maybe I am missing a complete logic here. Does anyone have any clue ?
Cheers,

I’m not 100% sure that I understand your problem completely, but I’ll try to give you some ideas.

  1. If you are trying to implement a
    system to be able to swap out
    powerups with similar logic, but
    different values, you can use
    Scriptable Objects.

  2. You can implement an interface (e.g.
    IPowerup), and use a state machine
    to swap out the different powerups.

  3. You can inherit from a base
    class, if you also want default
    values or behaviours.

  4. Finally, you can use Actions,
    which are similar to anonymous
    methods, and can be saved as
    variables. This way, just exchange
    the Action with another one to change the behaviour.


If you give me more information about your use case, I can give a more specific answer to your problem.