Search Unity

(SOLVED) Get specific script from all possible objects?

Discussion in 'Scripting' started by Denisowator, Sep 22, 2017.

  1. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    I have a DialogueManager script, and I need it to disable a DialogueTrigger script, so that while the text is showing, the player can't click the trigger button (in this case E) and set the dialogue box back to the first part of the dialogue.

    Each interactible object has the DialogueTrigger script attached to it, specifying what the dialogue is, how many parts is has, and a name text for who's talking.

    The way I understand it, is I need a sort of opposite to "FindObjectsOfType", because instead of finding objects with a specific script, I'm finding a script that's on specific objects. But I also need it to find the script instance on every object that has that script, rather than on a single object.

    Any ideas? I tried looking around but everything is about finding objects and not their scripts, or it's just about finding a script on a specific object. Whereas I need to find that script on all object that have it (just repeating to make sure I'm making myself clear).
     
    Last edited: Sep 28, 2017
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You could find all objects in the entire scene, and then iterate through them all to see if they have your DialogueTrigger component, but I think that is a terrible idea for performance reasons (as in terrible performance).

    What I would consider is to make your DialogueManager script, or another script you create for this purpose, to be a singleton in your scene. Give it a unique tag, so in your Start in your DialogueTrigger you can do a GameObject.FindObjectWithTag("DM") where DM would be the tag you'd give it. And whenever your player presses the trigger button (E in your case), it checks the DialogManager script if it is allowed to trigger.
     
    Denisowator likes this.
  3. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    Any ideas how I would actually set whether it's allowed to trigger or not?

    Also, I didn't mention, the thing that actually starts the dialogue is another script, which checks if "E" was pressed, and then calls a function in the DialogueTrigger script, which in turn calls a function in the DialogueManger (which starts the script).

    This is why I wanted to somehow disable the DialogueTrigger, so it cuts the process before it gets to the DialogueManager and manages to do anything.

    The whole DialogueManager runs on custom functions, so there's no Start or Update.

    Don't know if that matters, just thought I'd mention it.
     
    Last edited: Sep 22, 2017
  4. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    How about using a static variable in the script and checking for it.
     
  5. simonlvschal

    simonlvschal

    Joined:
    Nov 17, 2015
    Posts:
    266
    well in your DialogueManager. simple way would to add a bool that is true whenever you have that specific dialogue open. and then reset whatever you have to reset.

    you could also create a Event that runs when ever the dialogue is triggered and do your stuff in there.
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    FindObjectsOfType<Script> returns all instances of Script, not all GameObjects that has Script on them, so that's what you're looking for.

    But also don't do it like that, all of the Find methods searches linearly though every single object in your scene. You should instead get hold of the DialogueManager in the DialogueTrigger and check if there's an ongoing conversation.
     
  7. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    Okay got it working so far.

    I just ended up checking for a boolean before checking for key input. I set the bool to true in the declaration, and false once the key is pressed. Then in the DialogueManager I set the bool back to true once the dialogue has ended.

    Could you explain how checking for an ongoing conversation would work? In terms of checking which instance of a script (or object in this case) you're interacting with.
     
  8. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    Another method you could take, if you don't need this system to take in any possible kind of script type without having to modify them, is to simply have the scripts you want to get all of, to set their own reference into a public static list on void Awake(), and then your manager script can simply look at that variable and see all of those scripts/objects, and that list will automatically be updated whenever you instance a new object that has one of those scripts, since it will simply add itself to that global list.
     
  9. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    Link to solution

    I just ended up finding the closest object to the player, and setting the bool for that object's script.