Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Lost solver parameter from rope
#1
Hi!

I was duplicating a gameobjects that consists of an ObiSolver (parent) and a Rope (child) with 2 game objects (prefabs) with rigidbodies. The original gameobject has a script that I use to subscribe to the event OnCollision. That script requires the rope in order to perform a Tear, so that way I can simulate a cut in the rope. In that script I subscribe to that event doing the following:

Code:
m_Actualrope.solver.OnCollision += CutTheRope;

m_ActualRope is ObiRope type.
The issue starts when I duplicate the prefab, because it seems that the m_ActualRope is always null on that prefab clone.

Is that a bug?

Regards!
Reply
#2
(01-09-2020, 11:17 AM)flaurens Wrote: Hi!

I was duplicating a prefab that consists of an ObiSolver (parent) and a Rope (child) with 2 game objects with rigidbodies. The original prefab has a script that I use to subscribe to the event OnCollision. That script requires the rope in order to perform a Tear, so that way I can simulate a cut in the rope. In that script I subscribe to that event doing the following:

Code:
m_Actualrope.solver.OnCollision += CutTheRope;

m_ActualRope is ObiRope type.
The issue starts when I duplicate the prefab, because it seems that the m_ActualRope is always null on that prefab clone.

Is that a bug?

Regards!

Prefab management is entirely done by Unity, not Obi. This entirely depends on how you're setting the m_ActualRope variable in your script. Is this a public reference that you're dragging the rope into in prefab edit mode? Are you setting this variable trough code in Awake() or Start()?
Reply
#3
(01-09-2020, 11:24 AM)josemendez Wrote: Prefab management is entirely done by Unity, not Obi. This entirely depends on how you're setting the m_ActualRope variable in your script. Is this a public reference that you're dragging the rope into in prefab edit mode? Are you setting this variable trough code in Awake() or Start()?
Yes, it is a public variable, I drag the child (the rope) to the script that is located on the solver.
The subscription is done on the awake method.

PS: Obi solver and the rope are not prefabs, the two gameobjects with rigidbodies are the prefabs.
Reply
#4
(01-09-2020, 11:25 AM)flaurens Wrote: Yes, it is a public variable, I drag the child (the rope) to the script that is located on the solver.
The subscription is done on the awake method.

Hi,

I'm unable to reproduce any problems when doing this. I've added a public ObiRope variable to a custom script, placed the script onto a prefab with solver+rope, dragged the rope to the variable slot in the inspector. Then I instantiated the prefab at runtime using Instantiate() and the reference is kept. Anyway, this is all handled automatically by Unity, Obi does not use any custom serialization/prefab system of any kind.

Make sure you're not referencing anything that's outside the prefab (as the reference will be lost).

PS: make sure that the null reference is not taking place because of m_ActualRope, but because of m_ActualRope.solver.  The solver can be null if the actor instance hasn't been yet added to a solver, so if you try to access it in your scripts Awake() (which may be called before the rope's Awake() method, since the order in which methods are called for different components is undefined in Unity), the reference to the solver may as well be null. That's what Start() is for, so try subscribing in Start() instead of Awake().
Reply
#5
(01-09-2020, 11:38 AM)josemendez Wrote: Hi,

I'm unable to reproduce any problems when doing this. I've added a public ObiRope variable to a custom script, placed the script onto a prefab with solver+rope, dragged the rope to the variable slot in the inspector. Then I instantiated the prefab at runtime using Instantiate() and the reference is kept. Anyway, this is all handled automatically by Unity, Obi does not use any custom serialization/prefab system of any kind.

Make sure you're not referencing anything that's outside the prefab (as the reference will be lost).

PS: make sure that the null reference is not taking place because of m_ActualRope, but because of m_ActualRope.solver.  The solver can be null if the actor instance hasn't been yet added to a solver, so if you try to access it in your scripts Awake() (which may be called before the rope's Awake() method, since the order in which methods are called for different components is undefined in Unity), the reference to the solver may as well be null. That's what Start() is for, so try subscribing in Start() instead of Awake().
Yes srry, That's exactly what is happening. I explained myself really bad. The m_actualRope.solver is what is null. I will try your approach, thank you!

PD: Solved! Using Start method for init solves the issue.
Reply