var cs : CastShield = GetComponentInChildren(CastShield);
cs.CastShield(“kerpow”);
You might have an issue if the component you’re trying to find is more than one level below since I’m pretty sure GetComponentInChildren is not recursive (which is lame.) Having a function name the same as the script name is also risky.
You could also experiment with the message sending system:
You’re searching for a child object named “Shield” directly under your parent object… when in fact, its two children deep. Your code should look like this…
if (Input.GetKeyDown("e")) {
transform.Find("Spells/Shield").GetComponent(CastShield).CastShield("test");
}
thanks for the replies. Find(“”) as I understand looks in the entire scene for the GameObject so I dont think it would make any difference using Spells/Shield. I’ll try both, thanks.
Since you are using transform.Find and GetComponent every time anyway (and not storing a reference to the component somewhere), I suggest using BroadcastMessage instead. The performance will not be that different anyway, and your code will be simpler and more flexible.
Well, this is more of a relationship or association issue than an object oriented issue.
Regarding your OO question, if you have patterns that you’re used to in more familiar Objected Oriented languages like Java or C++, I’d recommend switching your scripting over to C#. The syntax is mostly like Java and everything from implementing interfaces to inheritance is done in a more familiar way. This Allowed me to design my interfaces and classes as I would in any other language/framework without worrying about how you emulate the concepts in JavaScript.
Makes for good encapsulation, reusability and decoupling.
However, there are probably more examples to get you started that are written in JavaScript.