我不能在無效更新中使用串列它說“問題在當前背景關系中不存在”
我嘗試在 void start 中制作它,它有效,但我必須在 void update 中進行。
有沒有辦法做到這一點?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class QController : MonoBehaviour
{
[SerializeField] string Qstring;
[SerializeField] Text Qtext;
[SerializeField] PlayerController PlayerController;
// Start is called before the first frame update
void Start()
{
string q1 = "Selimiye caminin mimar? bu camiyi ne olarak adland?rm??t?r?";
string q2 ="Selimiye Camiyi kim yapm??t?r?";
// ...
List<string> Questions = new List<string>();
Questions.Add(q2);
// ...
}
// Update is called once per frame
void Update()
{
// here it says Questions doesn't exist in the current context
Qstring=Questions[PlayerController.randomQnumber];
Qtext.text=Qstring;
}
}
uj5u.com熱心網友回復:
這應該可以解決問題。如您所見,可以訪問 PlayerController,但不能訪問 Questions。這僅意味著您應該將串列放置在 PlayerController 所在的相同位置。在嘗試更多之前,我會做一些編程教程來了解它為什么以及如何作業。這是基本的編程。
檢查 madmonk46 評論以獲得一個好的教程。
public class QController : MonoBehaviour
{
[SerializeField] string Qstring;
[SerializeField] Text Qtext;
[SerializeField] PlayerController PlayerController;
private List<string> Questions;
// Start is called before the first frame update
void Start()
{
string q1 = "Selimiye caminin mimar? bu camiyi ne olarak adland?rm??t?r?";
string q2 ="Selimiye Camiyi kim yapm??t?r?";
// ...
Questions = new List<string>();
Questions.Add(q2);
// ...
}
// Update is called once per frame
void Update()
{
// here it says Questions doesn't exist in the current context
Qstring=Questions[PlayerController.randomQnumber];
Qtext.text=Qstring;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/528939.html
標籤:C#unity3d
上一篇:C#洪水填充但閾值允許相似顏色?
