x


BCE0019: 'ButtonAction' is not a member of 'UnityEngine.Component'.

ButtonAction is a function of a script..

LinkedObj.GetComponent("Int_" + LinkedObj.name).ButtonAction(); //Calls Int_Object's ButtonAction function

As you see.. I MUST use the string parameter of GetComponent but I need to make it work on Android. All the loop-holes were using the Type parameter instead of String. I need a way to get it work as a string or convert ("Int_" + LinkedObj.name) into a component type.

more ▼

asked Mar 18 '12 at 04:55 AM

indGaming gravatar image

indGaming
0 1 1 1

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

1 answer: sort voted first

Unity doesn't support dynamic typing when porting to Android. If you're planning to go mobile, you should always include #pragma strict at the top of your JS files. I'd personally recommend doing this, anyway.

Quick and dirty solution: SendMessage():

LinkedObj.SendMessage("ButtonAction");

If you're interested in object-oriented programming, it offers a simple and performant solution: create an abstract "button" class with a virtual ButtonAction() method, so that you can create a variety of buttons which each do different things but can all be activated in the same way:

LinkedObj.GetComponent(MyButtonParentClass).ButtonAction();
more ▼

answered Mar 19 '12 at 01:00 AM

rutter gravatar image

rutter
5.2k 2 11

Thank you. What is the reason for using #pragma strict? Also, I do know a good amount on OOP. I learned a lot about it when I created an RPG in MS Visual Studio's Visual Basic when I was younger. Also the button isn't like a gui button, its a activation button in-game like a swtch or lever. But that clas idea should still work but its not what I'm aiming for, I read more into that SendMessage method and it will work fine for using any linkedobj ill be using. Again thank you.

Mar 19 '12 at 01:35 AM indGaming

#pragma strict forces the compiler to treat your code more carefully -- for example, you'll get more warnings and errors when you access variables or functions that don't exist. Sometimes this can slow down your development a bit, but it can also catch problems that would prevent you from deploying to other platforms.

In particular, dynamic typing is disabled when you're in strict mode.

Mar 21 '12 at 08:34 PM rutter
(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:

x2498
x1952
x665
x302
x123

asked: Mar 18 '12 at 04:55 AM

Seen: 530 times

Last Updated: Mar 21 '12 at 08:34 PM