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 Passing SO from one Script to another. (Question)

Discussion in 'Scripting' started by NixonAroboX, May 13, 2023.

  1. NixonAroboX

    NixonAroboX

    Joined:
    Oct 23, 2022
    Posts:
    12
    Hey guys, I am not sure if im at the right place here to ask a question.
    But I got a tiny problem and my research didnt lead to any result xd. Hope anyone can help me.
    So I have some ScriptableObjects that are Craftingrecipes for my game, they have ingredients with amount and each ingredient is an Item. So when I collect the Recipe, I need to collect the ingredients(items) and if I did so, I would get the result item.
    My RecipeSO is attached to a GameObject in the scene, because it’s a SO I can manually add Ingredients to my recipe from the inspector. This is important, because im working in a team and the non coding guy needs to change them aswell =).
    The most important part ist, when I pickup a recipe, some UI shows up, that projects the requested itemicon and amount.
    Because the RecipeSO ingredients are changed in the inspector (we have many recipes), I can’t just reference the RecipeSO, it wouldn’t know about the ingredients that are added in the inspector.. The only script that knows about what specific RecipeSO is on that RecipeGO is the Recipe script that has a slot for the RecipeSO. From the recipe script I can access the current RecipeSO and it’s list of ingredients easily, but I really want to access the current RecipeSO‘s ingredients list from another script that knows nothing about this my Recipe Script, so a UI Slot could know about the needed item, gets it’s sprite and take the requested amount from the ingredient and to it for each ingredient.. So something like publish and listen, but I’m really not sure how to to this.
    I hope anyone can help me ;/
     
  2. Nefisto

    Nefisto

    Joined:
    Sep 17, 2014
    Posts:
    324
    Run an action event passing your SO as a parameter, this could be run by your player (which I suppose will trigger the pickup event). Then you can register your UI as listener of this event and run show things as you desire
     
  3. NixonAroboX

    NixonAroboX

    Joined:
    Oct 23, 2022
    Posts:
    12
    i will try That, thanks a lot!
     
  4. NixonAroboX

    NixonAroboX

    Joined:
    Oct 23, 2022
    Posts:
    12
    Btw this worked perfect. Didn’t know much about dependency Injection. Now I know :)
     
    Nefisto likes this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
  6. CodeRonnie

    CodeRonnie

    Joined:
    Oct 2, 2015
    Posts:
    280
    Interfaces and events work well together. When a member accepts an interface, any object can easily implement that interface and be an accepted value to pass to that member. That constitutes loose coupling because the owner of that interface member only depends on the interface, not a particular concrete type. If the interface includes an event, then the receiver of the interface instance can subscribe to the event, and now the interface instance that was passed in can talk back to the owner object, and neither one of them has a strong type dependency on the other!
     
    Kurt-Dekker likes this.