為什么我的 bool 值設定只執行一次?然后回傳false?當我執行該函式時,它設定為“true”一次,然后回傳false。
private bool[] set = new[] { false, false, false, false};
void Update()
{
movefunc(setSpineRotates[0], "spinerotatecolor");
}
void movefunc(bool set, string co)
{
if (Input.touchCount == 1)
{
Touch screentouch = Input.GetTouch(0);
Ray ray = camera.ScreenPointToRay(screentouch.position);
if (screentouch.phase == TouchPhase.Began)
{
if (Physics.Raycast(ray, out RaycastHit hitInfo))
{
if (hitInfo.collider.gameObject.GetComponent(co) != null)
{
set = true;
}
}
}
}
Debug.log(set);
//. when i perform the function it, set to "true" once,then return to false.
if (set)
{
sliderx.SetActive(true);
slidery.SetActive(true);
sliderz.SetActive(true);
}
}
所以當我執行該功能時,它是否只復制值?
uj5u.com熱心網友回復:
當 bool 型別傳遞給函式時,它不會通過參考傳遞。該 bool 的一個新實體被創建,并且在該函式中,只有該實體被更改為 true。
要解決這個問題,您需要使用 ref 關鍵字傳遞它,或者使用 index[i] 作為引數,如下所示:
void movefunc(int index, string co)
{
...
if (hitInfo.collider.gameObject.GetComponent(co) != null)
{
set[index] = true;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/395717.html
上一篇:如何通過腳本更改音頻?統一
