![](/img/trans.png)
[英]Unity - Show Different attributes for different values of another variables in inspector
[英]Variables in script do not show in Inspector (Unity)
我正在嘗試學習如何使用Unity並遵循在線教程,但是當前遇到一個我不知道如何解決的問題。
我的場景中有一個Sprite,我已經附加了一個腳本,但是在Inspector中,它顯示了腳本,但是看不到里面的變量? 以前我遇到過這個問題,它已經解決了。
此問題的原因是什么/如何解決?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpaceShip : MonoBehaviour {
public float speed = 30;
public GameObject theBullet;
private void FixedUpdate()
{
float horzMove = Input.GetAxisRaw("Horizontal");
GetComponent<Rigidbody2D>().velocity = new Vector2(horzMove, 0) *
speed;
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("Jump"))
{
Instantiate(theBullet, transform.position, Quaternion.identity);
}
}
}
編輯:通過重新導入解決了問題。
您需要將變量聲明為Public
或[SerializeField]
才能使成員變量出現在檢查器中。 請注意,通過將某些內容聲明為public,可以從類外部(例如,從其他腳本/類)訪問變量。 默認情況下,將private
分配給成員變量。
例:
public class testscript : MonoBehaviour
{
public int foo; // shows up in inspector
[SerializeField] private int bar; // also shows up while still being private
void Start()
{
}
}
沒問題,您肯定會忘記做某事。
一開始,它在Unity中很常見。
重新開始。
在場景中創建一個新的GameObject並添加腳本。
如果檢查器顯示不可變:
要么
如果所有這些都足夠好以至於該變量是公共的,並且沒有超出腳本的方法范圍,以便在檢查器中看到該變量,則沒有什么秘密。
一個提示,不要在FixedUpdate或Update中使用GetComponent或Instantiate,因為它們很昂貴,請將Rigidbody2D保存在Start中的變量中,然后使用它。
對不起,我的英語好運。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.