Obi Official Forum
Help Particle Attachments at runtime - Printable Version

+- Obi Official Forum (https://obi.virtualmethodstudio.com/forum)
+-- Forum: Obi Users Category (https://obi.virtualmethodstudio.com/forum/forum-1.html)
+--- Forum: Obi Rope (https://obi.virtualmethodstudio.com/forum/forum-4.html)
+--- Thread: Help Particle Attachments at runtime (/thread-3073.html)



Particle Attachments at runtime - fishing-rod - 19-08-2021

Hi, I ran into a problem assigning and removing the targets of particle attachments at runtime.
I'm on Obi Rope 6.2 and Unity 2020.3.15f2.
I think I got it to work at some point, but I cant figure out the build in my collab history.

Whenever I attach something to the rope, i.e. setting the target property, I run this code so that everything sits at the right position:


Code:
/// <summary>
/// 1. Temporary saves the targets of the obi particles attachments.
/// 2. Sets the targets to null.
/// 3. Resets all line particles.
/// 4. Sets attachments to their original positions.
/// 5. Reassigns the targets to the attachments.
/// </summary>
public void ResetLine()
{
    // 1 In order to reset particles all attachment targets have to be removed.
    ObiParticleAttachment[] attachments = Rod.lineObj.GetComponents<ObiParticleAttachment>();
    Transform[] formerTargets = new Transform[attachments.Length];

    ObiRope rope = Rod.lineObj.GetComponent<ObiRope>();

    // 2
    for (int i = 0; i < attachments.Length; i++)
    {
        formerTargets[i] = attachments[i].target;
        attachments[i].target = null;
    }

    // 3
    rope.ResetParticles();

    // 4
    if (attachedHook != null)
    {
        attachedHook.transform.position = Rod.hookObj.transform.position;
    }

    if (attachedEquipment != null)
    {
        attachedEquipment.transform.position = Rod.equipObj.transform.position;
    }

    // Pause for debugging purposes
    EditorApplication.isPaused = true;

    // 5
    for (int i = 0; i < attachments.Length; i++)
    {
        attachments[i].target = formerTargets[i];
    }
}


I have a breakpoint at ResetParticles and the funktion is properly executed. The attached GameObjects (hook and equipment) are at their original positions when I pause the editor.
The rope particles are not though. I also still see the target properties being set in the inspector.

[Image: on-pause.png]

The screenshot is during the pause from code. The two red dots are the attached GameObjects, the line goes right through them in its original state.

There is some manual workaround which makes it work: When I de- and reactivate the ObiRope component during the pause and then resume,
the particles are back at their original positions. Recreating that in code with Rod.line.gameObject.SetActive() doesn't work though.

Am I approaching this in the wrong way?


RE: Particle Attachments at runtime - fishing-rod - 19-08-2021

I got it to work when adding


Code:
Rod.lineObj.GetComponent<ObiRope>().enabled = false;
Rod.lineObj.GetComponent<ObiRope>().enabled = true;


as the very last lines in the function.