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

How can I store a class I want to send a message to in a variable?

Discussion in 'Scripting' started by Draconic, Jun 15, 2014.

  1. Draconic

    Draconic

    Joined:
    Oct 12, 2013
    Posts:
    82
    Right now, my game is set up with a manager class, and a UI, Utility, Sound and Game class which come off of the manager class. For my button script, there are 2 variables -
    Message, containing the message.
    SendTo, containing a string which changes what to send it to.

    I do not want to keep the SendTo, and want to instead store the class I want to send to directly in the variable. This way I can add new classes, and also send messages to many other gameobjects.

    Help would be great, thanks !

    void OnMouseDown(){
    if(SendTo == "UI"){
    Manager.UI.SendMessage(message);
    }
    if(SendTo == "Game"){
    Manager.Game.SendMessage(message);
    }
    if(SendTo == "Utility"){
    Manager.Utility.SendMessage(message);
    }
    if(SendTo == "Sound"){
    Manager.Sound.SendMessage(message);
    }
    leaving = true;
    }
     
  2. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,613
    Code (csharp):
    1.  
    2. public GameObject SendTo;
    3.  
    4. /* … */
    5. SendTo.SendMessage(message);
    6. /* … */
    7.  
     
  3. Draconic

    Draconic

    Joined:
    Oct 12, 2013
    Posts:
    82
    Oh, thanks! That's alot simpler than what I was trying to do after posting this, thank you!