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

Question Assign a script to a ScriptableObject without knowing script name

Discussion in 'Scripting' started by Johan_Liebert123, Nov 26, 2022.

  1. Johan_Liebert123

    Johan_Liebert123

    Joined:
    Apr 15, 2021
    Posts:
    474
    Hello all, after a long break. I've started working on my game again
    I was wondering how can I assign a script in the ScriptableObject for each game mode, I want to assign a manager for each specific GameMode, so it's easy to access it out of my player script. What reference should I use, like I can't use GameObject, and can't use that specific game modes manager script name, because it's different for each Game mode, is this possible?
     
  2. iMobCoding

    iMobCoding

    Joined:
    Feb 13, 2017
    Posts:
    160
    You can try with interfaces. Search some c# interface tutorials for help
     
    Johan_Liebert123 likes this.
  3. Johan_Liebert123

    Johan_Liebert123

    Joined:
    Apr 15, 2021
    Posts:
    474
    I know a bit about Interfaces, but how will that help with my problem aren't interfaces used for set properties for something, like I've used an interface for damage, really common. But how is that going to be able to help? I don't think it will help me set the game mode manager etc, or am I mistaken? Please correct me if so.
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,881
    Interfaces or a base class should work, and just make derived types of said base class to define different game modes. OOP 101.
     
    Johan_Liebert123 likes this.
  5. Johan_Liebert123

    Johan_Liebert123

    Joined:
    Apr 15, 2021
    Posts:
    474
    Im not following at all, I'm not trying to define different game modes, I'm trying to assign a script to ScriptablObject without knowing the name of it.
    Any link to documentations would be great!
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,881
    What the hell does 'assign a script' mean? Because it doesn't mean anything in the context of C#.
     
  7. Johan_Liebert123

    Johan_Liebert123

    Joined:
    Apr 15, 2021
    Posts:
    474
    Assign a script to a scriptable object. Let's say in the script I write
    Code (CSharp):
    1. public GameManager gameManager
    I would like to assign that script to the ScriptableObject, which I can't. In my case I want to add a different script per ScriptableObject, like so
    upload_2022-11-28_11-25-11.png
    1. Is it possible, if not, what alternative should I use to define a game mode manager script for each ScriptableObject
    2. What type would I use to add a different script per object
     
  8. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,881
    Right, so you mean, 'assign a reference to an instance'. Scripts are the assets that live in your assets folder.

    Well you in both cases you need a common scriptable object and a common game manager type. So basically...
     
    Johan_Liebert123 likes this.
  9. Johan_Liebert123

    Johan_Liebert123

    Joined:
    Apr 15, 2021
    Posts:
    474
    Yeah yea that. I still feel like you're not understanding me, or I'm not understanding you. There is a different game mode manager for each game mode. What I am trying to achieve is to get a reference of that game mode manager from my player.cs. I can't directly assign that game mode script to my player nor can I find that type using GetComponent<T>()
    So now after thinking about it, I don't think assigning the reference to the SO object would be great, because the values etc aren't really added. So I need to get it from the Scene. I do have a variable for the current game mode, referencing the SO, but I'm not sure how I can find that script in the scene
     
  10. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,881
    Oh I understand you, but you're not understanding some aspects of C#. If you want to get a solid reference to something you need a concrete type/implementation. If you want something to be generalised, you need a common base class or an interface that defines common expected functionality. That's how C# works.

    If your game modes are all separate, unrelated scripts, then you may need to consolidate that and do the suggested above (base class/interfaces). Otherwise, you'll have to hard code a search for each particular type when entering each game mode. Neither option is strictly worse or better than one another.

    There are countless patterns to globally access something, such as a singleton. And when all else fails,
    FindObjectOfType<T>
    works.
     
    Johan_Liebert123 likes this.
  11. Johan_Liebert123

    Johan_Liebert123

    Joined:
    Apr 15, 2021
    Posts:
    474
    I can't use that because I can't do
    Code (CSharp):
    1. FindObjectOfType<CurrentGameMode.Name>();
    lol
    My player knows which GameMode it is, but doesn't have a reference to that specific GameMode manager which is alive in the scene
     
  12. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,881
    Yes... hence literally everything I've said previously that you seem intent to ignore.

    I've explain the solution well enough; you haven't seemed to have digested any of it so I'm out.
     
  13. Johan_Liebert123

    Johan_Liebert123

    Joined:
    Apr 15, 2021
    Posts:
    474
    I'm not ignoring, not at all lol. I'm figuring my way through that, as soon as I do a couple of attempts I'll let you know
     
  14. iMobCoding

    iMobCoding

    Joined:
    Feb 13, 2017
    Posts:
    160
    What we're trying to tell you is, all those different managers you want to assign (like GameManager, GameManager2, LevelManager, etc...) must have something in common if you want to reference them as a single object in your ScriptableObject.

    For example, let's say all have common fields like currentLevel, currentGameMode, etc... and then few common methods like LoadNextLevel(), KillThePlayer(), etc... When you find all those common properties then you have two options: 1. make an Interface that defines all those commons or, like @spiney199 also suggested, 2. make a base class that has all those commons and then create subclasses for all those different managers. Once you decide and implement those things then you'll be able to reference a single object in your ScriptableObject and assign whichever manager you need in the inspector.
     
    Chubzdoomer and Johan_Liebert123 like this.
  15. Johan_Liebert123

    Johan_Liebert123

    Joined:
    Apr 15, 2021
    Posts:
    474
    Thanks for that, makes much more sense.
    But the problem is that they dont have common references. The thing is I already have a GameManager.cs. Which manages deaths, loading etc. Though I also have a different script for each GameMode which lies long with GameManager.cs, different in each scene. They manage the game modes gameplay, they have nothing in common what so ever.
     
  16. iMobCoding

    iMobCoding

    Joined:
    Feb 13, 2017
    Posts:
    160
    I understand. Then maybe it's a right time to rethink the whole structure of your game and handle everything differently...
     
    Reedex likes this.