Unity C#报错:public不是有效修饰符、OutputStory不存在于当前上下文
问题原因
两类报错的核心根源是语法错误:你将OutputStory、ManageStory、OnOpt1等所有自定义方法,全部写在了Update方法的内部,违反了C#的语法规则:
- C#不允许给定义在方法内部的局部方法添加
public/private这类访问修饰符,你定义的5个带public的按钮回调方法全部触发“public不是该项的有效修饰符”错误 - 局部方法的作用域仅限包含它的方法内部,你在
Start方法中调用OutputStory时,该方法仅存在于Update的作用域中,自然会提示“名称OutputStory不存在于当前上下文”
修复方案
将Update方法的结束右大括号},移动到check == true的判断逻辑结束位置,让所有自定义方法和Start、Update平级,作为Game类的成员方法即可。修正后的完整代码如下:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro; using UnityEngine.SceneManagement; public class Game : MonoBehaviour { [SerializeField] StoryState startingStory; [SerializeField] StoryState specialEnding; string storyText; [SerializeField] TextMeshProUGUI storyTextBox; StoryState currentStory; [SerializeField] Image myStoryImage; [SerializeField] ParticleSystem endingParticles; bool check = false; [SerializeField] GameObject amongus; int rand; float x; float y; private void Start() { endingParticles.Stop(); currentStory = startingStory; OutputStory(); } private void Update() { if (check == true) { if (rand == 1) { amongus.transform.localScale += new Vector3(x, y, y + x) * Time.deltaTime; } else if (rand == 2) { amongus.transform.Rotate(new Vector3(x, y, x * y) * Time.deltaTime); } else if (rand == 3) { amongus.transform.localScale += new Vector3(x, y, x / y) * Time.deltaTime; amongus.transform.Rotate(new Vector3(x, y, x - y) * Time.deltaTime); } else if (rand == 4) { amongus.transform.localScale += new Vector3(x * x, y, x / y) * Time.deltaTime; amongus.transform.Rotate(new Vector3(x, y / y, x - y) * Time.deltaTime); } else if (rand == 5) { amongus.transform.Rotate(new Vector3(x, y, x - y) * Time.deltaTime); } else if (rand == 6) { amongus.transform.Rotate(new Vector3(x, y, x / y) * Time.deltaTime); } else if (rand == 6) { amongus.transform.Rotate(new Vector3(x, y * y, x * y) * Time.deltaTime); } else if (rand == 7) { amongus.transform.Rotate(new Vector3(x, y / y, x * y) * Time.deltaTime); } else if (rand == 8) { amongus.transform.localScale -= new Vector3(x / x, y * x, y + x) * Time.deltaTime; } else if (rand == 9) { amongus.transform.localScale -= new Vector3(y * x, y / x, y - x) * Time.deltaTime; } else if (rand == 10) { amongus.transform.localScale -= new Vector3(y * x, y / x, y - x) * Time.deltaTime; amongus.transform.Rotate(new Vector3(x, y / y, x * y) * Time.deltaTime); } else if (rand == 11) { amongus.transform.localScale += new Vector3(x, y, x * y) * Time.deltaTime; amongus.transform.Rotate(new Vector3(x, y, x + y) * Time.deltaTime); } else if (rand == 12) { amongus.transform.localScale += new Vector3(x, y, x * y) * Time.deltaTime; amongus.transform.Rotate(new Vector3(x, y, x - y) * Time.deltaTime); } else if (rand == 13) { amongus.transform.localScale += new Vector3(x, y, x * y) * Time.deltaTime; amongus.transform.Rotate(new Vector3(x, y, x - y) * Time.deltaTime); } } } void OutputStory() { storyText = currentStory.GetStoryText(); storyTextBox.text = storyText; myStoryImage.sprite = currentStory.GetSceneImage(); } void ManageStory(int option) { var nextOptions = currentStory.GetStoryOption(); if (option > nextOptions.Length - 1) { Debug.Log("Test"); storyText = "That is not an option"; Invoke("OutputStory", 1); } else { currentStory = nextOptions[option]; OutputStory(); } } public void OnOpt1() { ManageStory(0); } public void OnOpt2() { ManageStory(1); } public void OnOp3() { ManageStory(2); } public void OnAMUNGUS() { Debug.Log("Amungus"); if (currentStory.name == "E9 - Reported") { currentStory = specialEnding; OutputStory(); endingParticles.Play(); check = true; rand = (int)Random.Range(1f, 13f); Debug.Log(rand); x = Random.Range(-100f, 100f); y = Random.Range(-100f, 100f); } } public void OnExitPress() { SceneManager.LoadScene("MainMenu"); } }
可选优化点
- 代码中存在两个
rand == 6的重复判断分支,第二个分支永远不会触发,建议调整 Random.Range(1f, 13f)转为int时取值范围为1~12,你写的rand == 13分支永远无法触发,若需要包含13可修改为Random.Range(1, 14)(整数重载的Random.Range为左闭右开规则)
内容的提问来源于stack exchange,提问作者Zaxoosh
相关产品推荐
相关产品推荐

