如何让spiritvalue在其他类中也可以拜访到public class Spritcontroller MonoBehaviour public int spiritvalue = 25; private Animator animator; Start is called before the first frame update void Start
要让其他类也能访问到spiritvalue,可以将spiritvalue声明为public static,这样其他类就可以直接通过类名来访问spiritvalue的值。修改代码如下:
public class Spritcontroller : MonoBehaviour
{
public static int spiritvalue = 25;
private Animator animator;
// Start is called before the first frame update
void Start()
{
animator = GetComponent
// Update is called once per frame
void Update()
{
if (spiritvalue == 0)
{
animator.SetFloat("Blend", 0);
}
if (spiritvalue == 5)
{
animator.SetFloat("Blend", 0.1f);
}
// 其他条件判断省略...
}
}
然后在其他类中,可以通过Spritcontroller.spiritvalue来访问该值。例如:
public class OtherClass : MonoBehaviour { void Start() { int value = Spritcontroller.spiritvalue; // 其他逻辑... }
原文地址: https://www.cveoy.top/t/topic/inc2 著作权归作者所有。请勿转载和采集!