Unity3D - 尽管我在检查器上设置了什么,为什么变量始终为真?
Unity3D - Why is a variable always true despite what I set on the inspector?
我已将变量设置为 SerializeField 以从检查器中选择值。但是,该值没有改变,它始终是 "true" 值。
我试过public,更改名称,重新导入组件,重置组件,......似乎没有任何效果,唯一有效的是改变Awake 方法中的值。
public class AudioController : MonoBehaviour
{
[SerializeField] private bool playSoundsAtColision; //WHY IS TRUE??? Is false in the inspector...
private void OnCollisionEnter2D(Collision2D collision)
{
if (this.playSoundsAtColision)
Debug.Log("playSoundsAtColision = " + this.playSoundsAtColision + " by: " + gameObject.name, gameObject);
}
}
这里有整个代码的图像:https://i.imgur.com/rT5BdhT.png
(你可以看到变量只被声明,然后被检查和打印,没有其他地方可以访问它)
尽管我在检查器上设置了什么,但这里会发生什么:https://i.imgur.com/FLFugq8.png
提前感谢您的帮助!
检查是否:
- 当游戏以单机模式运行时,您正在挂起 属性 - 当您退出单机模式时,属性会重置
您没有在代码中设置 属性。要更改布尔值的默认值 (true),您需要为字段 playSoundsAtColision 设置一个值:
public class AudioController : MonoBehaviour
{
[SerializeField] public bool playSoundsAtColision = false; //you just shoud change the variable to public and default value to false or true
private void OnCollisionEnter2D(Collision2D collision)
{
if (this.playSoundsAtColision)
Debug.Log("playSoundsAtColision = " + this.playSoundsAtColision + " by: " + gameObject.name, gameObject);
}
}
问题是该组件存在多次。
我检查过预制件没有两个重复的组件,但预制件变体有。我还在适应中。
正如 AresCaelum 所说,使用 "DisallowMultipleComponent" 属性应该有助于避免此类问题。
我已将变量设置为 SerializeField 以从检查器中选择值。但是,该值没有改变,它始终是 "true" 值。
我试过public,更改名称,重新导入组件,重置组件,......似乎没有任何效果,唯一有效的是改变Awake 方法中的值。
public class AudioController : MonoBehaviour
{
[SerializeField] private bool playSoundsAtColision; //WHY IS TRUE??? Is false in the inspector...
private void OnCollisionEnter2D(Collision2D collision)
{
if (this.playSoundsAtColision)
Debug.Log("playSoundsAtColision = " + this.playSoundsAtColision + " by: " + gameObject.name, gameObject);
}
}
这里有整个代码的图像:https://i.imgur.com/rT5BdhT.png (你可以看到变量只被声明,然后被检查和打印,没有其他地方可以访问它)
尽管我在检查器上设置了什么,但这里会发生什么:https://i.imgur.com/FLFugq8.png
提前感谢您的帮助!
检查是否:
- 当游戏以单机模式运行时,您正在挂起 属性 - 当您退出单机模式时,属性会重置
您没有在代码中设置 属性。要更改布尔值的默认值 (true),您需要为字段 playSoundsAtColision 设置一个值:
public class AudioController : MonoBehaviour { [SerializeField] public bool playSoundsAtColision = false; //you just shoud change the variable to public and default value to false or true private void OnCollisionEnter2D(Collision2D collision) { if (this.playSoundsAtColision) Debug.Log("playSoundsAtColision = " + this.playSoundsAtColision + " by: " + gameObject.name, gameObject); } }
问题是该组件存在多次。
我检查过预制件没有两个重复的组件,但预制件变体有。我还在适应中。
正如 AresCaelum 所说,使用 "DisallowMultipleComponent" 属性应该有助于避免此类问题。