Hi there.
How can I call a function within a object?
I mean, I have this function “x” on “blah.js”, and I’ve attached it to “myObj”.
How can I do something like GameObject.Find(“myObj”).x()?
Hi there.
How can I call a function within a object?
I mean, I have this function “x” on “blah.js”, and I’ve attached it to “myObj”.
How can I do something like GameObject.Find(“myObj”).x()?
My UnityScript is rusty, but will GameObject.Find(“myObj”).GetComponent(“blah”).x() work?
Yes.
But can I do it without the “GetComponent”?
Thx
Unless “x” is a public, static method/field, I don’t think you can. It’s just the way Unity is structured (and it’s to your benefit). Because if you have two scripts, “foo” and “bar”, and they both have a method “x”, if you didn’t specify which component it’s on (through GetComponent) which “x” would it invoke?
You can always store a reference to the script instance and keep calling it after:
var myTargetObject = GameObject.Find("myObj");
var myBlah = myTargetObject.GetComponent("blah");
myBlah.x();
myBlah.SomethingElseThatBlahDoes();
if (myBlah.MyCondition)
{
//do something
}
Alright then.
Thanks! ![]()