23-11-2020, 03:01 PM
(This post was last modified: 23-11-2020, 03:02 PM by JoseCarlos S.)
(23-11-2020, 01:56 PM)josemendez Wrote: Hi there,
If you run this script, it will result in a NullRefException error (visible in the console), and the script will halt execution.
This is because of this line:
Code:emitter.speed = 0;
"emitter" will always be null because you only require a component of type ObiSolver to be present:
Code:[RequireComponent(typeof(ObiSolver))]
But then you try to retrieve a ObiEmitter component, which is more than likely to be absent. So this call to GetComponent will return null:
Code:emitter = GetComponent<ObiEmitter>();
Easiest fix is to just make the emitter member public, then assign the emitter instance trough the inspector:
Code:using UnityEngine;
using Obi;
[RequireComponent(typeof(ObiSolver))]
public class KillFluidOnContact : MonoBehaviour
{
ObiSolver solver;
public ObiEmitter emitter;
public float targetTime = 2.0f;
void Awake()
{
solver = GetComponent<ObiSolver>();
}
private void Update()
{
targetTime -= Time.deltaTime;
if (targetTime <= 0.0f)
{
timerEnded();
}
}
void timerEnded()
{
if (emitter != null)
emitter.speed = 0;
else Debug.LogWarning("No se ha proporcionado un emisor a este script.");
}
void LateUpdate()
{
if (!isActiveAndEnabled)
return;
// iterate over all particles in the solver, looking for particles
// that have had their diffusion data altered by contact with other particles.
for (int i = 0; i < solver.particleToActor.Length; ++i)
{
// if this particle is part of an actor,
if (solver.particleToActor[i] != null)
{
// if the "user" (aka diffusion) data is not either (1,0,0,0) or (0,0,0,0)
if (solver.userData[i].x < 0.9f && solver.userData[i].x > 0.1f)
{
// take a reference to the emitter that the particle belongs to, and kill it.
var emitter = solver.particleToActor[i].actor as ObiEmitter;
emitter.KillParticle(solver.particleToActor[i].indexInActor);
}
}
}
}
}
This is basics of the basics, tough. You should train your programming skills a bit further .
It keeps spawning, when all particles are destroyed. Doesn´t seem to change emitter speed... Yeah, this is my training!