Hello,
I have several scripts attached to game objects, and each game object has also a Particle System
(as prefab) attached to it via serialized field visible in inspector.
In the scripts, a ThrusterController class is instantiated with new. One version is working, but a slightly
different one leads to a NullReferenceException. Would be nice I someone could explain it why that is,
I am new to Unity. Thanks.
Working class implemtation (of a script attached to a game object):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HorizontalRearThrusterController : MonoBehaviour
{
[SerializeField] ThrusterController controller = new ThrusterController();
private KeyCode keyCode;
private void Awake()
{
}
void Start()
{
controller.ThrusterOrientation = 1;
controller.ParticleEmitterOffsetOnThrustReversal = 0f;
controller.IncThrustKey = KeyCode.E;
controller.DecThrustKey = KeyCode.R;
var main = controller.ThrusterParticle.main;
main.startLifetimeMultiplier = 2f;
main.simulationSpeed = 0;
controller.ThrusterParticle.Play()
}
void Update()
{
controller.ProcessCommand(gameObject);
}
}
Class implentation with NullReferenecException on highlighted line:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HorizontalRearThrusterController : MonoBehaviour
{
[SerializeField] ThrusterController controller = null;
private KeyCode keyCode;
private void Awake()
{
controller = new ThrusterController(),
}
void Start()
{
controller.ThrusterOrientation = 1;
controller.ParticleEmitterOffsetOnThrustReversal = 0f;
controller.IncThrustKey = KeyCode.E;
controller.DecThrustKey = KeyCode.R;
[COLOR=#ff0000] var main = controller.ThrusterParticle.main;[/COLOR]
main.startLifetimeMultiplier = 2f;
main.simulationSpeed = 0;
[COLOR=#ff0000] controller.ThrusterParticle.Play()[/COLOR]
}
void Update()
{
controller.ProcessCommand(gameObject);
}
}
Here the ThrusterControllerClass:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class ThrusterController
{
[SerializeField] ParticleSystem thrusterParticle;
private int thrust=0;
private int thrustDirection = -1;
private int thrustMin = -10;
private int thrustMax = 10;
private float particleEmitterOffsetOnThrustReversal = 0;
private int thrusterOrientation = 0;
private KeyCode incThrustKey;
private KeyCode decThrustKey;
public ThrusterController()
{
}
public ParticleSystem ThrusterParticle
{
get { return thrusterParticle;}
}
public int Thrust
{
set { thrust = value; }
get { return thrust; }
}
public KeyCode IncThrustKey
{
set { incThrustKey = value; }
get { return incThrustKey; }
}
public KeyCode DecThrustKey
{
set { decThrustKey = value; }
get { return decThrustKey; }
}
public int ThrustMin
{
set { thrustMin= value; }
get { return thrustMin; }
}
public int ThrustMax
{
set { thrustMax = value; }
get { return thrustMax; }
}
public int ThrusterOrientation
{
set { thrusterOrientation = value; }
}
public void SetThrusterParticleSpeed()
{
thrusterParticle.playbackSpeed = Mathf.Abs(thrust) / 10.0f;
if (thrust == 0) thrusterParticle.Clear();
}
public void RelocateVerticalThruster(GameObject thruster, int thrustDirection, float offset)
{
thruster.transform.Rotate(thrustDirection * 180, 0, 0);
float move = thruster.transform.localPosition.y;
float _x= thruster.transform.localPosition.x;
float _z= thruster.transform.localPosition.z;
move += offset*thrustDirection;
thruster.transform.localPosition = new Vector3(_x, move, _z);
}
public void RelocateHorizontalThruster(GameObject thruster, int thrustDirection, float offset)
{
thruster.transform.Rotate(thrustDirection * 180, 0, 0);
float move = thruster.transform.localPosition.z;
float _x = thruster.transform.localPosition.x;
float _y = thruster.transform.localPosition.y;
move -= offset * thrustDirection;
thruster.transform.localPosition = new Vector3(_x, _y, move);
}
public void ProcessCommand(GameObject thruster)
{
if (Input.GetKeyDown(IncThrustKey))
{
Debug.Log(IncThrustKey);
if (Thrust < ThrustMax)
{
Thrust++;
SetThrusterParticleSpeed();
}
if (thrustDirection == 1 && Thrust > 0)
{
thrustDirection = -1;
if(thrusterOrientation==0) RelocateVerticalThruster(thruster, thrustDirection, particleEmitterOffsetOnThrustReversal);
else RelocateHorizontalThruster(thruster, thrustDirection, particleEmitterOffsetOnThrustReversal);
}
}
else if (Input.GetKeyDown(DecThrustKey))
{
if (Thrust > ThrustMin)
{
Thrust--;
SetThrusterParticleSpeed();
}
if (thrustDirection == -1 && Thrust < 0)
{
thrustDirection = 1;
if (thrusterOrientation == 0) RelocateVerticalThruster(thruster, thrustDirection, particleEmitterOffsetOnThrustReversal);
else RelocateHorizontalThruster(thruster, thrustDirection, particleEmitterOffsetOnThrustReversal);
}
}
}
public float ParticleEmitterOffsetOnThrustReversal
{
set { particleEmitterOffsetOnThrustReversal = value; }
}
}