My problem is, when I attached my script to a game object, I want to make some calculation and add Edge Collider to this game object. Is there any event like "OnAttached" or something else?
Thanks for all your helps.
Thanks to Ruben I found the solution. RequireComponent is what I was looking for, but I actually needed an event like "OnAttached" and at last I found. It is "void Reset()".
The automated addition of the Edge Collider can be done by using [RequireComponent(typeof(EdgeCollider))]
Learn more here:
https://docs.unity3d.com/ScriptReference/RequireComponent.html
Edit: OP says this wasn't helpful, refer to his comment below for more information:
You then can simply put all necessary calculations into the Start() functions, which get called, when a script is enabled.
https://docs.unity3d.com/ScriptReference/MonoBehaviour.Start.html
Related
I'm trying to add the Unity Button Script to a 3d gameObject. When I tried this by adding it onto a cube, I can't get the desired behaviours. How can I get the 3d object to work like a button? In particular the highlighting colors, the click events. I was speculating that perhaps it uses the wrong raycaster, and if the raycaster specificaiton is changed some how it would work? Does anyone have a good enough understanding of the source code to explain if this is possible, and if so what I would need to do to achieve this? Thanks.
Rik
You could do it by using OnMouseDown() function. Here is the link Unity Docs
You will also need to add the image script along with the button script.
How do I detect Input.GetMouseButtonUp outside a specific GameObject's area in Unity? Are there any Unity Assets for this, or should we do this programmatically?
It certainly has to be done by programming it, whether there is an asset or not. As it is rather easy to program, I doubt there is one.
What you need to understand is, that Input.GetMouseButtonUp(int button) is an event and entirely decoupled from any "position". The description from the API says "Returns true during the frame the user releases the given mouse button.".
So in the frame a mouse button has been released you want to check if the arrow is "touching" or laying over some GameObject (GO). According to your example you will do nothing when the GO was hit, and else do something.
One way to do that is to use the following method:
http://docs.unity3d.com/ScriptReference/Camera.ScreenPointToRay.html
Your GameObject will at least need a collider, maybe a rigidbody too in order to be detected by the ray. I'm not completely sure atm if both are needed.
Additional note: Having an UI element, this method could also be used to set some flag while hovering over it e.g. : http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseOver.html
Currently when I am trying to detect whether an object has been clicked / interacted with by the mouse, I create a script on that GameObject and then define void OnMouseDown() and apply functionality within. If I then want to trigger a function in another script that is not attached to the GameObject I call it through a GetComponent<>() reference.
Is there a way to add a callback to the OnMouseDown event from a script on another GameObject? Potentially in the same way delegates / events are allocated and invoked? Something such as: gameObjectReference.GetComponent<BoxCollider>().onmousedownevent += MyFunction; So that it automatically calls this function on mouse down?
Note: I know that you can use raycasting to achieve something similar, but my question is about attaching callbacks to the existing Unity event.
1) I understand the sense of what you mean by this
pseudocode:
gameObjectReference.GetComponent<BoxCollider>().onmousedownevent += MyFunction
and there is no way to do that.
2) please do understand: it is incredibly easy to achieve your aim (elegantly). Add a UnityEvent in script "b", Invoke it in the OnMouseDown. drag anything you want there. For anyone reading who is new to Unity, pls try UnityEvent in general as it is used constantly
3) A I mention you can kind of do what you want by fooling around with the OnPointerDown(PointerEventData eventData) systems, but this just emphasises you can't do what you ask, do it with OnMouseDown.
Sorry for the grim news.
I'm using LeanTween engine and all was going well until I tried this:
LeanTween.moveY (menuPanel, 1200f, 0.5f)
.setEase (LeanTweenType.easeInOutBack);
The goal is to have the menu panel move down slightly before shooting up out of the screen. The easeInOutBack seemed the correct type (based on Easing Cheatsheet).
menuPanel is a GameObject UI Panel of which I declare as public and assign in the inspector.
Instead it goes a little crazy, bouncing up and down sporadically whilst moving a bit.
Has anyone observed this behaviour before, know what I'm doing wrong?
I know this is an old question, but just in case anyone else is having similar problems - you'll need to disable the Rigidbody attached to the game object you're tweening to avoid getting unpredictable behaviour in your raycast.
Im making a main menu, and I want to make the 3D object as a button to move to another scene. The 3D object is jar import from SKETCHUP. Im a newbie thats why im still dont know. can someone give me example code using c#?? the name of the object is JAR. thankyou in advance:)
This is how you do it:
Apply a Collider on your object (or you could make it with raycast but it's not necessary for menu buttons)
Define a OnMouseDown or OnMouseUp event. By the way, learn to use Unity documentation, it even has what you're looking for in this example: MonoBehaviour.OnMouseDown().
This is the code to handle click event: (taken directly from the provided link)
void OnMouseDown () { // or OnMouseUp
Application.LoadLevel("level name"); // or level index
}
I assume you know how to define different scenes so you can reference them at loading. If not, read this for a basic guide on how it's done: Application.LoadLevel.
Also a note: Unity has a really big community forum on it's own and has lots of active members which have much more experience. But please, do your homework and watch the tutorials and also try to search for your problem before spamming with questions that have already been explained. Nobody likes to help someone who doesn't want to do any effort on it's own.