RotateSpeedDial.ShowSpeed
равно static
, поэтому вам вообще не нужно получать ссылку на него через GetComponent
.
Просто используйте его как
// Reference this via the Inspector and you don't need GetComponent at all
public Rigidbody rb;
// As a fallback get it ONCE on runtime
private void Awake()
{
if(!rb) rb = GetComponent<Rigidbody>();
}
public void Update()
{
RotateSpeedDial.ShowSpeed(rb.velocity.magnitude, 0, 100);
}
Тем не менее, у вас есть своего рода шаблон Singleton , который весьма спорный ... вы должны ссылаться на него через инспектора.
И Find
, и GetComponent
нельзя использовать в определении поля в классе. Также ScriptHolder
нельзя использовать для инициализации поля в классе, так как оно присваивается самой среде выполнения, а не значением c. Вам нужен какой-то метод, например, Awake
или Start
.
Поэтому у вас будет, например,
// Reference all these via the Inspector using drag&drop
public Rigidbody rb;
// If you drag this in via the Inspector you wouldn't need the "ScriptHolder"
// at all.
public RotateSpeedDial rotateSpeedDial;
public GameObject ScriptHolder;
// Or as a fallback get them all on runtime ONCE like
// I use a little rule of thub: Get and initialize your own stuff at Awake
private void Awake()
{
if(!rb) rb = GetComponent<Rigidbody>();
}
// Get other objects stuff on Start
// This way you can almost always be sure that the other stuff
// is already initialized when you access it
private void Start()
{
if(!rotateSpeedDial)
{
if(! ScriptHolder) ScriptHolder = GameObject.Find("SpeedDial");
rotateSpeedDial = ScriptHolder.GetComponent<RotateSpeedDial>();
}
// Or simply use
if(!rotateSpeedDial) rotateSpeedDial = FindObjectOfType<RotateSpeedDial>();
}
public void Update()
{
rotateSpeedDial.ShowSpeed(rb.velocity.magnitude, 0, 100);
}
, и тогда вы вообще не будете использовать static
в RotateSpeedDial
:
float minAngle = 70.64f;
float maxAngle = -269.69f;
public void ShowSpeed(float speed, float min, float max)
{
var ang = Mathf.Lerp(minAngle, maxAngle, Mathf.InverseLerp(min, max, speed));
transform.eulerAngles = Vector3.forward * ang;
}