Calling a button onClick action from another script in Unity - unity3d

I've been stuck on this for a little while. I want to call an onClick action for a button from another script.
I.E. see the below capture. I want to call that onClick from another script.
I tried this.transform.GetChild(0).SendMessage("onClick", number); but I keep getting SendMessage onClick has no receiver! error.
Any idea what I am missing?
Thank you in advance :)

Are you sure that you are sending the message to right GameObject? In your case the Main Camera, not the button. Your code works in my unity 4.6.2. You can print the name of the GameObject with this:
Debug.Log(this.transform.GetChild(0).gameObject.name);
If the GameObject is the right, and the message doesn't still work you can always get the component and call the function old fashion way without messages:
this.transform.GetChild(0).gameObject.GetComponent<ShopBtns>().onClick(number);

Related

I want to change MRTK3 Ray Interactor Input Action Change in Unity

I am learning MRTK3 in Unity, and it is very similar to XRI.
I bind Select(Input Action) with gripPressed because I am using it in GrabInteractor.
but I realize Ray Interactor is working with Select(Input Action), and I delete triggerPressed in Select(Input Action). I can't RayInteract with object in trigger button. but RayInteract works in grip button. And I want to RayInteract with trigger button.
In Short, I bind UIPress(Input Action) with triggerPressed. but I don't know how can I change Input Action in RayInteract...
I already know how to change in XRI. but I think MRTK3 little bit different.

No function dropdown in AnimationEvent in Unity

I try to handle some events during animations, but everywhere I look, every tutorial have access to AnimatorEvent Inspector like this:
A nice simple field, where you can select a function, I want this!
But instead of this, I always getting this sick 5 fields view, and don't have any idea how to handle animation event in this case!
I tried to create function test() with debug log, but it didn't work anyway. Why I can't get access to this simple window where I can choose an function?
You will need to add this animation into a State in Animator Controller (via Animator Window).
In the object which contains Animator component, attach your script component to it.
Open the Animation window, select the object above, you will see a dropdown of animations (top left) which Animator Controller of this object contains. Choose and add event to the animation you want to.
Select the event in Animation Window, in the Inspector, you should see the dropdown of public functions of your script component attached above.
Answer by Aluminium18
Sometimes it doesn't work even if you do all things according to tutorials or advices in internet. I often leave Unity editor launched for a long time without any interactions with it. After several gibernations and several days it can get buggy - various errors appear, you can't see some functions from scripts and so on. So, just reloading the Unity editor solves many of such issues for me. And it continues to happen so for several years no matter what Unity version you have. I tried versions from 2019.x.x to 2022.x.x - all this time Unity behaves itself the same.

Issue with setting AutomationElement value

I have an issue with setting value of AutomationElement by using method ValuePattern.SetValue().
Everything works just fine until some dialog appears. When the dialog appears the code execution got stuck. No exception is thrown. After the dialog is confirmed, the code exection continues. Bellow is a sample of the code:
BasePattern basePattern = null;
ValuePattern valuePattern = null;
AutomationElement elementA = Window.GetElement(SearchCriteria.ByText(propertyName));
object patternObjectA = null;
elementA.TryGetCurrentPattern(ValuePattern.Pattern, out patternObjectA);
basePattern = (BasePattern)patternObjectA;
valuePattern = (ValuePattern)patternObjectA;
valuePattern.SetValue(optionToSet);
// Window.GetElement() is a method from TestStack.White framework
// The code execution got stuck on the last line until the dialog is confirmed
Is there any other way to set AutomationElement value?
Is somehow possible to avoid of getting stuck by dialog?
I'll by grateful for any help.
Thanks advance.
It could be that this dialog is not supporting UI Automation correctly or that you simply target the wrong element.
To verify that you may use Inspect.exe from Microsoft or similiar tools.
If it works, check if you really target the correct component with your code again.
If it does not work and:
if you are able to change the application
you can change the so called AutomationPeer of the UI component - here is a link for more infos
Or simply use another UI component that supports UI Automation correctly.
if you are not able to change the application, and also do not need to run in background, parallel, etc.. you might just focus the component (call setFocus() onto the AutomationElement, or expand it (via IsExpandCollapsePatternAvailable or simulated MouseClick onto the components coordinates)) and then use the SendKeys.SendWait("test") method.
EDIT: There is one more thing you should have a look at, and I wonder why I didn't mentioned it in the first place: Register to UI Automation Events
For example you could register a callback for the Structure change event type, and check if the dialog you talk about appeared.
If so --> click the confirmed button of the dialog.
Probably you will have to synchronize your execution, so that every further action in the UI Automation script waits until the registered callback got executed and the confirmed button got clicked.

Unity UI button

I have a multiplayer 2D game made in Unity and I wanted to put some onscreen buttons to be able to control it on the telephone. The problem is that when I put the Event Trigger on the button it uses the function from the script that has to use, but it can't open other functions called in the first one. For example, when I press the FireBullet Buton it should use a function CmdFireBullet() that has in it the function RpcFireBullet(). The function CmdFireBullet() works perfectly, but it does not open the function RpcFirebullet() at all. Any ideeas?
There is no RpcFireBullet inside CmdFireBullet
Just don't forget to type RpcFireBullet(); inside your CmdFireBullet

how to bind OnClick event to Singleton function?

I want to bind OnClick event of UI Button to a function in Singleton object. I tried to do that, but every time I go to other scene and get back to the old scene I find that object disappear from the onClick field in the inspector while it's exist in the hierarchy ! If it's not possible to do that, what's the alternative way ?
Note: I'm using c#
Looks like this guy had a similiar problem: http://answers.unity3d.com/questions/335736/gameobject-still-destroyed-after-reloading-level.html
I would have posted this as a comment if I had enough reputation..