Search Unity

Passing a class as object (SendMessage)

Discussion in 'Scripting' started by wyliam, Apr 7, 2009.

  1. wyliam

    wyliam

    Joined:
    Mar 28, 2009
    Posts:
    61
    Since SendMessage will only accept one object, I'm taking the suggestions from other postings to pass a class instead. Everything is lined up, however, I'm not sure how to plug that class in with the SendMessage method.

    Currently I have the following code. I'd like to access the properties of the instanced class (that are set in the myMethod function).

    Code (csharp):
    1. MyClass myClass = SendMessage("myMethod", m);
    I get the usual "cant implicitly convert type 'void' to 'MyClass'".

    Clearly they aren't the same type, but I expect myMethod to return the instance of MyClass.
    Do I need syntax forcing it to "typeof" or something else?
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    You don't pass a class, but an object of a class of your choice

    Code (csharp):
    1. MyClass m = new MyClass();
    2. // do whatever you want
    3. SendMessage("myMethod", m);
    Don't forget that you must cast the object back to MyClass in your "myMethod" function.
     
  3. wyliam

    wyliam

    Joined:
    Mar 28, 2009
    Posts:
    61
    yeah- As I stepped away from it for a bit, I realized that. Thanks for pointing it out. I'll give it another shot...
     
  4. wyliam

    wyliam

    Joined:
    Mar 28, 2009
    Posts:
    61
    Actually- after looking at it closer, this isn't quite what I'm trying to accomplish.

    In short, I have three scripts attached to the same object. Script A iterates through all the loaded textures and instantiates them as GameObjects. Each of these instances require a unique component.

    Script B contains a case statement (temporary fix) that defines which instances should get which prefab.

    Since I can't call Script B's function to instantiate with the components (and other properties) with the 3 arguments using SendMessage, I've defined a class (Script C) that has all these properties in one, with the intent of setting those, and then reading them in Script A.

    Clearly I can't pass the class itself- so should I create a game object, add Script C and then reference it's properties once I pass the object instance? Ultimately I'd have to destroy it too I suppose.

    Sounds like a lot of sleight of hand. Any ideas on a simpler way of approaching this?
     
  5. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    Yup! Give a reference to script B on script A. Since they're on the same object, that's not particularly hard.

    var scriptB : ScriptBName;

    Just drag the object onto the variable in the inspector and you'll be able to call functions like this:

    scriptB.FunctionName(arg1, arg2, arg3);
     
  6. wyliam

    wyliam

    Joined:
    Mar 28, 2009
    Posts:
    61
    Man- was I overcomplicating this... sheesh!