how to bind OnClick event to Singleton function? - unity3d

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..

Related

UE5 Create Event issues

I am having issues creating an event inside a function.
My function calls a “Bind Event to On Destroyed”. When dragging out the Event node, I call Create Event. After dragging the output of my bind event actor into the Create Event “Object” input, I select “Create a matching event” from the dropdown.
Afterwards, a new custom event appears inside my function, let’s call it “RespawnEvent”:
Now, when I search for the event in my “Create Event” dropdown, it just doesn’t appear:
Is this a bug or am I doing something wrong here?
Thanks in advance!

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.

How to trigger an event as soon as we launch the view in SWT

I have an RCP application where I am creating a view which invokes an instance of the composite.Now my problem is I want to trigger one event as soon as I show the view/composite in the screen.
I tried with addFocusListner(),addMouseTrackListener(),addMouseListner()but unfortunately none of the them gets the control as I move my mouse pointer around the view.Is there any way we can solve it?
Just grab the control you have used to create view and add selection listener on it.
for example if treeViewer is used to create that view then:
treeViewer.setSelection(new StructuredSelection(element),true);
Ensure selection by this treeViewer.getControl.setFocus();
If you want to trigger an event after your view is in focus/launch then you should add a listener to that view e.g IPartListener2for that you need to create a class and implement IPartListener2 interface. you will get more information here
Also if you want only using mouse event then you need to add MouseMoveListener to your view so when mouse pointer on your when mouseMove will call.
e.g. control.addMouseMoveListener(this);

Calling a button onClick action from another script in Unity

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);

lwuit container

I have added a container named btnBar with boxlayout(x-axis) on a form. the container has 4 buttons of custom class MyButton that extends from Button itself. When i add actionlistener to one of the buttons in the container it gets invoked for each n every button. Even the actionevent.getsource.gettext method returns the same value irrespective of the focus. I have added the buttons through an array and m trying to use is something like this:
btns[0].addActionListener(......)
Where am I going wrong?
got is solved buddies...actually was using setfocus() to traverse within the btnBar on buttons...but needed to use setFocused() method of form too....after setFocus()....dnt hammer ur brains now....
You must never use setFocus, you should use setFocusable to toggle focusability and requestFocus to get the focus to a specific component.
You should also migrate to Codename One, since LWUIT is pretty much unmaintained.