The Unity Manual describes the order in which the Script functions are called. However, I was wondering if there were any rules regarding the order in which the GameObjects themselves are considered in Unity.
GameObjects are basically the nodes of Unity's scene graph and (assuming the scene itself was the root node) they form a tree. I was wondering if that tree structure imposed any rules on the order in which GameObjects are considered.
As already mentioned, the manual describes that Awake() is always called before Start() which is always called before the first call to Update() and so on. However, these relations in time are (mostly) given in scope of a single script on a single GameObject. I want to know if there is also a rule stating the order in which Start() (or any other method) is called on all the GameObjects in the scene.
Specifically I wanted to know:
Are parents always considered before their children are?
Are siblings considered in the same order they are displayed in the scene graph?
Is Script Execution Order enforced only in scope of a single GameObject, or does it consider all GameObjects?
I built a small test project in Unity which basically consists of a 3x3x3 tree of GameObjects, each having 3 scripts.
I found the following answers:
No. Some GameObjects can be considered before their parents are, while some parents can be considered before their children are. And this order can change when reloading the scene or manipulating the scene graph.
No. Siblings can be updated in any order. This order can change when reloading the scene or manipulating the scene graph.
It is enforced over all GameObjects in the scene. If SEO sets script A to be executed before script B, then all instances of script A will be considered before any instance of script B is. Meaning, all instances of A call their Awake() before any B call their Awake(), then all instances of A call their Start() before any instance of B call their Start() and so on.
Related
An object has a trigger and its purpose, when an object is present, performed an action, and if there are no objects, another action
OnTrigger does not work, as it works on contact
You can subscribe to the following messages:
OnTriggerEnter: OnTriggerEnter is called when the Collider other
enters the trigger.
OnTriggerExit: OnTriggerExit is called when the
Collider other has stopped touching the trigger.
Proceed as follows:
From OnTriggerEnter, add the object that entered to a list(*)
From OnTriggerExit, remove the object that left from that list.
Then at any time, the list will contain every element that's inside the trigger.
From Update, do your action depending on whether the list is empty or not.
Note: I'm using the term « list » as in « collection of your choice ». It makes sense to use a HashSet<> rather than a List<> if the trigger can overlap with a significant number of objects.
EDIT: I see your question mentions "Unity 2D". If that means you just need 2D collisions, consider using Collider2Ds, and its 2D counterparts to the aforementioned hooks OnTriggerEnter2D and OnTriggerExit2D
I'm new to unreal engine, I'm trying to add large force to an object with a box collider but after it collide with other object (just another instance) the overlap inside each others and become like one object and moving with each others.
Cab anyone explain their behavior and how i should resolve this?
What happens here is that both objects collide with each other continously. To fix that you could try to deactive the OnOverlap()-Event on either the overlapping Object or the colliding object.
In blueprints you can achieve that by setting the Generate Overlap Events-Variable of one of the colliding static meshes of the overlapping objects to false.
In C++ you could simply remove the dynamic event callback for one of the colliding objects like that:
CollidingComponent->OnComponentBeginOverlap.RemoveDynamic(this, &ACollidingActor::OnBeginOverlap);
Where CollidingComponent is the component of your object, which causes the overlap event to trigger.
Like #Alex said, they overlap with each other, over and over. If you didn't know, you can add breakpoints to your blueprint nodes, just like in your code, by right-clicking a node and select Enable Breakpoint (or smth like that). Your game will stop when reaching it and switch to that exact point in your blueprint. You can then hover over that node and see every variables value going in and out of it.
Hope this helps you learing to use the Unreal Engine.
In my scene I have 2 camera(split screen). it's possible change the trasparency of a layer only for one camera? for examples the "layer1" have alphatrasparency = 0.5 for right camera while left camera show "layer1" without trasparency.
Ostensibly no
There's a way to do it, though. It's a bit of a hack though, as it doesn't depend on physics layers, but rather the presence of a custom monobehavior script. Here's what I remember about this off the top of my head (I can dig up an implementation later, if needed).
Step 1: you will need a MonoBehaviour script attached to every game object you want to have rendered differently. This script is absolutely essential.
Step 2: this script will contain one function (you can use an existing behaviour script if all the objects already have one in common and you can just add this function to that script). Call it whatever you want, but something like AboutToBeRendered(Camera cam)
Step 3: create another script and attach it to both cameras. This script will also have one function in it: OnPreRender()
Step 4: In this OnPreRender method you will need to do:
find all game objects from Step 1
get their component with the AboutToBeRendered method
invoke that method, passing the camera as the paramter
Step 5: Writing the AboutToBeRendered method.
determinie which of the two cameras was passed to the method
set the material's color to be transparent or not, as needed
OnPreRender() is only called on scripts with a camera component on the same GameObject, indicating that this camera is about to render the scene. But what we actually want is for the object about to be rendered to know that it's about to be rendered and by which camera. Which is why we need the script in step 1.
I suppose you could skip step 1 and only look at every object in the scene and look at its physics layer, but that's going to be more expensive to figure out than "get me all instances of this component." You could do it based on Tag instead, as FindObjectsWithTag is generally considered to be pretty fast, but if you're already using the tag for something else, you're out of luck, and there's no comparable method for getting objects in a given physics layer.
However, you'd have to tweak the material's alpha value in the camera script rather than letting the object itself decide what value it should be.
In either case, the object's material would need to support transparency. When I did this I was preventing the object from being rendered entirely, so I just disabled its MeshRenderer component.
I want to access some game Objects' Components i have 2 approaches:
A script on a GameObject(GO) accessing many GOs using FindGameObjectsWithTag("SomeTag"); but this not working for some GOs
Same Script instances (not sure it is called instance) on all that GOs i want to access their components.
My questions is that which approach is good, optimized and more sustainable for work.
And also give me alternative besides above 2 approaches if possible.
From your given approaches:
A script on a GameObject(GO) accessing many GOs using FindGameObjectsWithTag("SomeTag"); but this not working for some GOs
Probably the game objects you think not working would be
inactive, since AFAIK, FindGameObjectsWithTag finds active game objects
only.
Same Script instances (not sure it is called instance) on all that GOs i want to access their components.
You should make decision based on number of gameobjects that are under consideration:
If they are limited:
You can create an script like GameObjectControllerScript (attached to camera or some root gameobject, so their is only one instance of this script, [you are establishing one to may relation]), containing the references of all the game objects whose specific component you want to access and access the components on any event of your interest an dperform some operation on it.
e.g.,
Camera
-- Root //<---- Attached controller script here that contains reference of StaticallyCreatedGameObject1..4
----StaticallyCreatedGameObject1
----StaticallyCreatedGameObject2
----StaticallyCreatedGameObject3
----StaticallyCreatedGameObject4
If they are un-limited or generates dynamically:
This way you can not keep reference before, so you can attach a script say ChangeAngleScript (to game objects that are under consideration) which accesses component of a game object and perform some operation using function updateAngle, and broadcast a message of updateAngle from some root game object.
Camera
-- Root // <--- Broadcast message from here
----DynamicallyCreatedGameObject1 // <-- Attach `ChangeAngleScript` here
----DynamicallyCreatedGameObject2 // <-- Attach `ChangeAngleScript` here
----DynamicallyCreatedGameObject3 // <-- Attach `ChangeAngleScript` here
----DynamicallyCreatedGameObject4 // <-- Attach `ChangeAngleScript` here
What is the difference between those two methods? Why should i prefer one?
1)
GameObject.FindGameObjectWithTag("").GetComponent<Rocket>().active = true;
2)
GameObject.FindGameObjectWithTag("").GetComponent<Rocket>().SendMessage("setActive");
thanks!
Sending a message searches through all the components of the gameObject and invokes any function that has the same name as the message. Not a 100% sure but Im sure this uses reflection which is generally considered slow.
SetActive() or the active variable set the gameObject as active or not. If its not active it wont render in the scene and vice versa.
First of all it seems there are several inconsistencies with your code above:
1) Components (and MonoBehavior) don't have an active property (active belongs to GameObject), so the first line of code shouldn't compile. In addition the most recente version of unity don't have active anymore, it's substitued with activeSelf and activeInHierarchy.
And btw, both activeSelf and activeInHierarchy are read only, so you can't assing directly a value to them. For changing their value use SetActive method.
2)
The second line of code shouldn't work either (unless Unity does some magic behind the scenes) because SetActive methods belong to GameObject and not to your Rocket Component.
Now, I suppose your question was the difference between:
gameObject.SetActive(true);
and
gameObject.SendMessage("SetActive",true);
The result is the same, but in the second way Unity3D will use reflection to find the proper method to be called, if any. Reflection has an heavy impact on performance, so I suggest you to avoid it as much as possible.