unity3d 我只能在void start中使用名为“Questions”的列表

ui7jx7zq  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(108)

我无法在无效更新中使用列表,它显示“问题在当前上下文中不存在”
我试过在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;
    }
}
erhoui1w

erhoui1w1#

这应该可以解决这个问题。正如你所看到的,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;
    }
}

相关问题