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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Trying to get a script's function to be called from dynamically instantiated objects.

Discussion in 'Scripting' started by Kiloku, Mar 20, 2015.

  1. Kiloku

    Kiloku

    Joined:
    Mar 20, 2015
    Posts:
    4
    I have a "Hype.cs" script that controls a text label on the screen, and a "Blip.cs" that controls the behavior of the instances of a certain prefab called Blip.

    Blip.cs has this:

    Code (csharp):
    1.  
    2.     void OnTriggerStay(Collider other)
    3.     {
    4.             if (Input.GetButtonDown("Fire1"))
    5.             {
    6.                 //Tell Hype.cs to fire function addHype(1)
    7.             }
    8.     }
    9.  
    My problem is that I can't simply give Blip.cs a Hype object and drag the text object into the Blip object's script through the editor, as every instance of the Blip object is instantiated dynamically from a prefab. So I simply don't know what to do. I was looking for some ideas from Global Variables (and couldn't understand how to use Singletons or the Toolbox for that at all) to Events and EventSystem (And got the feeling that it isn't what I want)
     
  2. edwin_s

    edwin_s

    Joined:
    Mar 10, 2015
    Posts:
    19
    If I understand you correctly....
    Code (csharp):
    1. class Hype {
    2.     void Fire(GameObject other) {
    3.     }
    4. }
    5.  
    6. class randomObject {
    7.     void OnTriggerStay(Collider other) {
    8.         if (Input.GetButtonDown("Fire1")) {
    9.             //Tell Hype.cs to fire function addHype(1)
    10.             GameObject myText = GetComponent<Text>();
    11.             Hype.Fire(myText );   /// Not sure how to call the function, but the point is that you pass your relevant GameObject to it
    12.         }
    13.     }
    14. }
    15.  
    That should be the gist of it.
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    If there's only one Hype object, you could mark it as a singleton. The cheap version is simply:

    Code (csharp):
    1. public class Hype() : MonoBehaviour {
    2.     public static Hype instance;
    3.  
    4.     void Awake() {
    5.         instance = this;
    6.     }
    7. }
    And then in Blip, you do:

    Code (csharp):
    1. void OnTriggerStay(Collider other) {
    2.         if (Input.GetButtonDown("Fire1")) {
    3.            Hype.instance.addHype(1);
    4.         }
    5. }
    There are other ways to do this, and arguably better ways to do this, but this will work for sure.
     
  4. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    If you're instantiating the Blips from a prefab, you can simply get the Blip component from your new object:

    Code (csharp):
    1.  
    2. GameObject newBlipObject = Instantiate(blipPrefab);
    3. Blip newBlip = newBlipObject.GetComponent<Blip>();
    4.  
    Then, just assign to members of newBlip or call methods on it.