x


Detecting presence of, and calling functions in, another script

I have a script which needs to do two things:

(1) Detect the presence on another script in my project (2) If the other script exists, call a function in it

I need to do this to make two libraries I am releasing seperatley work with each other if they are both present in the same Unity project.

What would be the best way to accomplish this? So far I am playing with reflection, obscure OO ideas, eval inside a try/catch block, script 2 storing a function reference for itself inside a static variable of script 1, etc... I have a feeling I am, as usual, over thinking this.

more ▼

asked Dec 18 '11 at 02:12 AM

Aubrey Falconer gravatar image

Aubrey Falconer
657 45 53 68

(comments are locked)
10|3000 characters needed characters left

2 answers: sort oldest

Yep! I was overthinking it :) gameObject.AddComponent ("OtherLibrary"); worked perfectly. Thanks for your ideas!

more ▼

answered Dec 18 '11 at 08:11 AM

Aubrey Falconer gravatar image

Aubrey Falconer
657 45 53 68

Nice. I bet my silly test would have broken if tested further...

Would probably throw an exception if SuperFunkyClass was NOT defined.

Nice work!

Dec 18 '11 at 08:53 AM jahroy
(comments are locked)
10|3000 characters needed characters left

Well....

I've never done anything like that, but since you asked, I tried this:

class SuperFunkyClass
{
    var funkyVar : String;

    static function getFunky () : void
    {
       Debug.Log("Let's get funky");
    }
}

function Start ()
{
    if ( SuperFunkyClass  &&  SuperFunkyClass.getFunky ) {
       SuperFunkyClass.getFunky();   
    }

    if ( SuperFunkyClass  &&  SuperFunkyClass.getNifty ) {
       SuperFunkyClass.getNifty();   
    }

    else {
       Debug.LogWarning("SuperFunkyClass can't get Nifty...");
    }
}

It seems to do what I believe you describe.

more ▼

answered Dec 18 '11 at 02:34 AM

jahroy gravatar image

jahroy
3.2k 14 18 41

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4

asked: Dec 18 '11 at 02:12 AM

Seen: 240 times

Last Updated: Dec 18 '11 at 08:53 AM