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
Related
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.
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.
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..
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);
I'm still pretty new to scripting in Unity3D, and I'm following along with a tutorial that uses GUI.Button() to draw a button on the screen.
I am intrigued by how this function works. Looking through the documentation, the proper use of GUI.Button is to invoke the function in an if statement and put the code to be called when the button is pushed within the if statement's block.
What I want to know is, how does Unity3D "magically" delay the code in the if statement until after the button is clicked? If it was being passed in as a callback function or something, then I could understand what was going on. Perhaps Unity is using continuations under the hood to delay the execution of the code, but then I feel like it would cause code after the if statement to be executed multiple times. I just like to understand how my code is working, and this particular function continues to remain "magical" to me.
I don't know if it's the right term, but I usually refer to such system as immediate mode GUI.
how does Unity3D "magically" delay the code in the if statement until
after the button is clicked?
GUI.Button simply returns true if a click event happened inside the button bounds during last frame. Basically calling that function you are polling: every frame for every button asking the engine if an event which regards that button (screen area) is happened.
If it was being passed in as a callback function or something, then I
could understand what was going on
You are probably used to an MVC like pattern, where you pass a controller delegate that's called when an UI event is raised from the view. This is something really different.
Perhaps Unity is using continuations under the hood to delay the
execution of the code, but then I feel like it would cause code after
the if statement to be executed multiple times.
No. The function simply returns immediately and return true only if an event happened. If returns false the code after the if won't be executed at all.
Side notes:
That kind of system is hard to maintain, especially for complex structured GUI.
It has really serious performance implications (memory allocation, 1 drawcall for UI element)
Unless you are writing an editor extension or custom inspector code, I'd stay away from it. If you want to build a menu implement your own system or use an external plugin (there are several good ones NGUI, EZGUI,..).
Unity has already announced a new integrated UI System, it should be released soon.
Good question. The unity3d gui goes through several event phases, or in the documentation
Events correspond to user input (key presses, mouse actions), or are UnityGUI layout or rendering events.
For each event OnGUI is called in the scripts; so OnGUI is potentially called multiple times per frame. Event.current corresponds to "current" event inside OnGUI call."
In OnGUI you can find out which event is currently happening with >Event.current
The following events are processed link:
Types of UnityGUI input and processing events.
-MouseDown
-MouseUp,mouse button was released
-MouseMove,Mouse was moved (editor views only)
-MouseDrag,Mouse was dragged
-KeyDown, A keyboard key was pressed
-KeyUp A keyboard key was released.
-ScrollWheel The scroll wheel was moved.
-Repaint A repaint event. One is sent every frame.
-Layout A layout event.
-DragUpdated Editor only: drag & drop operation updated.
-DragPerform Editor only: drag & drop operation performed.
-DragExited Editor only: drag & drop operation exited.
-Ignore Event should be ignored.
-Used Already processed event.
-ValidateCommand Validates a special command (e.g. copy & paste).
-ExecuteCommand Execute a special command (eg. copy & paste).
-ContextClick User has right-clicked (or control-clicked on the mac).
Unity GUI has much improved lately and is quite usefull if you want to handle things programmatically. If you want to handle things visually, i recommend looking at the plugins heisenbug refers to.
If you decide to use unity gui, i recommend using only one object with ongui, and let this object handle all your gui.