18-03-2018, 05:51 PM
(This post was last modified: 18-03-2018, 05:52 PM by josemendez.)
(18-03-2018, 05:46 PM)Gieve Wrote: So I came across the same issue regarding my code where I need to generate ropes with balls on the end of them from a height and length set up before rendering the ropes. the ropes generate correctly but I have been unable to pin the prefab ball to the ropes as they generate previously I had the issue before with IEnumerators and [] indexing, but your tip on using GetFirstBatch helped solve that, yet now it is giving me a NullReferenceException.
I know the reference to the instantiated object is correct because I used it to set the material of the ball.
Here I believe the offending code is.
Code:using UnityEngine;
using System.Collections;
using Obi;
namespace Obi
{
[RequireComponent(typeof(ObiRope))]
[RequireComponent(typeof(ObiCatmullRomCurve))]
[RequireComponent(typeof(ObiPinConstraints))]
public class RopeMaker : MonoBehaviour {
public ObiSolver solver;
public ObiRopeSection section;
public Material material;
public Vector3 start;
public Vector3 end;
public int ropeNum;
private ObiRope rope;
private ObiCatmullRomCurve path;
public IEnumerator MakeRope () {
// Get all needed components and interconnect them:
rope = GetComponent<ObiRope>();
path = GetComponent<ObiCatmullRomCurve>();
rope.Solver = solver;
rope.ropePath = path;
rope.section = section;
GetComponent<MeshRenderer>().material = material;
// Calculate rope start/end and direction in local space:
Vector3 localStart = transform.InverseTransformPoint(start);
Vector3 localEnd = transform.InverseTransformPoint(end);
Vector3 direction = (localEnd-localStart).normalized;
// Generate rope path:
path.controlPoints.Clear();
path.controlPoints.Add(localStart-direction);
path.controlPoints.Add(localStart);
path.controlPoints.Add(localEnd);
path.controlPoints.Add(localEnd+direction);
// Setup the simulation:
yield return StartCoroutine(rope.GeneratePhysicRepresentationForMesh());
rope.AddToSolver(null);
// Fix first and last particle in place:
rope.invMasses[0] = 0;
Oni.SetParticleInverseMasses(solver.OniSolver,new float[]{0},1,rope.particleIndices[0]);
//Set Phase:
for (int i = 0; i < rope.TotalParticles; ++i) {
rope.phases [i] = Oni.MakePhase (ropeNum, 0);
rope.PushDataToSolver (ParticleData.PHASES);
yield return new WaitForFixedUpdate ();
}
}
public void AddPendulum(ObiCollider pendulum, Vector3 offSet){
ObiPinConstraints pins = rope.GetComponent<ObiPinConstraints>();
ObiPinConstraintBatch batch = pins.GetFirstBatch()[0] as ObiPinConstraintBatch;
pins.RemoveFromSolver (null);
batch.AddConstraint (rope.UsedParticles - 1, pendulum, offSet, 0);
pins.AddToSolver (null);
pins.PushDataToSolver ();
}
}
}
Change this:
Code:
ObiPinConstraintBatch batch = pins.GetFirstBatch()[0] as ObiPinConstraintBatch;
to this:
Code:
ObiPinConstraintBatch batch = pins.GetFirstBatch() as ObiPinConstraintBatch;
pins.GetFirstBatch() returns an object, not an array.