Hi everyone! I’m stucked.
i’m creating instatiate of complex object and trying to set parameters from parent to childs, like:
main script:
public static void some_func()
{
objects_list.Add(Instantiate(new_object)
}
new_object script:
void Awake()
{
some_static_parameter = 1;
}
new_object_sub_object script:
void start()
{
GetComponent<Rigidbody2D>().position = new vector2(new_object.some_static_parameter,0)
}
And some time it works (3 object in a row created correctly), and some time it doesn’t (4th will not get it), sub_object stay on default place. Can I use constructions as I mentioned, or it must be organized other way?
I will really appreciate your help.
Based on your second comment, your original problem is solved but now you want to understand why the solution worked as it did, so here it goes:
The position of Rigidbody, documented here, is a way of informing the physics system of the new location you want, which is then used by the physics system to move the object’s transform, in some way relevant to the physics being applied to the object. If your object doesn’t use physics, then the Ridigbody.position would have no opportunity to alter the transform’s position.
Ultimately, it is the transform that defines the position and orientation of the object. The Rigidbody is a means of getting the physic engine to move the object, where moving the object means updating the object’s transform properties for position and rotation. When you switched to moving the transform’s members, you went for that means of control compatible with the Unity engine design, but with caveats. If the object you’re moving is also controlled by physics, altering the object’s transform conflicts with the physics engine’s control of that object (your code and the engine argue over that control). To solve that argument, Unity provides position in the Rigidbody so you have a means of providing some input to the physics engine for it’s application of control over the object’s transform. When physics is not used on the object, you can ignore the Rigidbody and merely use the object’s transform.
In your case, however, it appears you had intermittent response when using Rigidbody.position. It is likely that there was some physics event happening coincident (and conflicting) with your expectation, because using position is not absolute positioning, it is relative to what the physics engine is doing to that object.
Typically, however, when instantiating new objects, you set the transform as a means of initialization. Your instantiation happens in a cycle of events where physics isn’t going to have control of that object until after you’ve had the opportunity to initialize it, and that includes setting the transform’s members, but for objects controlled by physics this is a one time opportunity. Once you ‘let go’ of the object within the Update or FixedUpdate function that instantiates an object, physics may take over, begin to control the object, and you should generally avoid using the transform for subsequent manipulation.