[已解决]如何根据状态更改画布上的文本。 [英] [SOLVED] how do I change text on canvas based on states.

查看:66
本文介绍了[已解决]如何根据状态更改画布上的文本。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候!



我正在我的短文冒险游戏\练习结束时,他调试了它,就我目前认为的那样。足够让它让我在Unity中测试它。我提出的初始文本加载得很好,但没有一个键意味着转换到其他更多文本的场景响应。



我记得我有一些东西需要准确,但不是确切地说,使画布从指定的c#代码中读取文本字符串。

请求帮助Unity工作环境的屏幕截图,如下所示 - 以及代码的第一部分枚举和定义状态。



工作环境: [ ^ ]



尝试时控制台错误读数测试: [ ^ ]



以下代码:



Greetings!

I'm at the end of my short text adventure game \ practice and he debugged it as far as I currently think I can. Enough that it lets me play test the thing in Unity. The initial text I put up loads just fine, but none of the keys meant to transition to other scenes with more text respond.

I remember there was something I had to do, but not what it was, exactly, to make the canvas read text strings from the c# code specified.
Requesting assistance with screenshots of Unity work environment to follow below - and the first bits of the code that enumerate and define states.

Work environ : [^]

Console error readouts on attempting to test : [^]

Code below :

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Game : MonoBehaviour {

	public Text text;
	
	private enum states {begin, light, d1, d2, applaud, deal, cave, memories, reflection, path1, egg, TH, no6, ocean, bEnd1, credits, summer, phone, memory, basement, weed, upstairs, devil, awake, bEnd2, computer, bEnd3, GOLDEN, GOLDEN2}
	private states myState;

	// Use this for initialization
	void Start () {
		myState = states.begin;
	}
	
	// Update is called once per frame
	void Update () {
		print (myState);
		if (myState == states.begin) {
			begin(); }
		else if (myState == states.light) {
			light(); }
		else if (myState == states.cave) {
			cave(); }
		else if (myState == states.path1) {
			path1(); }
		else if (myState == states.egg) {
			egg(); }
		else if (myState == states.TH) {
			TH(); }
		else if (myState == states.ocean) {
			ocean(); }
		else if (myState == states.bEnd1) {
			bEnd1(); }
		else if (myState == states.summer) {
			summer(); }
		else if (myState == states.basement) {
			basement(); }
		else if (myState == states.upstairs) {
			upstairs(); }
		else if (myState == states.awake) {
			awake(); }
		else if (myState == states.bEnd2) {
			bEnd2(); }
		else if (myState == states.computer) { 
			computer(); }
		else if (myState == states.bEnd3) {
			bEnd3(); }
		else if (myState == states.GOLDEN) {
			GOLDEN(); }
		else if (myState == states.d1) {
			d1(); }
		else if (myState == states.d2) {
			d2(); }
		else if (myState == states.applaud) {
			applaud(); }
		else if (myState == states.deal) {
			deal(); }
		else if (myState == states.memories) {
			memories(); }
		else if (myState == states.reflection) {
			reflection(); }
		else if (myState == states.egg) {
			egg(); }
		else if (myState == states.no6) {
			no6(); }
		else if (myState == states.credits) {
			credits(); }
		else if (myState == states.phone) {
			phone(); }
		else if (myState == states.memory) {
			memory(); }
		else if (myState == states.GOLDEN2) {
			GOLDEN2(); } 
			
	}


	void begin () {
	text.text = "This is like a bad dream. You don't know how you got here, where this place is, or why you got here, but you know that you feel uneasy. " +
				"Something is very wrong with all of this. You should try and look for a way out of...wherever this is! \n \n  " +
				"You could go and see about that [L]ight in the distance, investigate the sound coming from a nearby [C]ave, or head down the nearby [P]ath..." ;
	if (Input.GetKeyDown(KeyCode.L)) 		{myState = states.light;}
	else if (Input.GetKeyDown(KeyCode.C))	{myState = states.cave;}
	else if (Input.GetKeyDown(KeyCode.P))	{myState = states.path1;}
	}







-----------------



我是Unity和C#编程的新手,请保持答案简单。



简而言之:游戏不响应状态按钮。导入的音频文件引发错误,控制台错误信息让我感到不安。



INFO:Unity版本4.6.9F1





谢谢!



我尝试过:



捏造代码,一些UI交互更改,将脚本资产文件拖到各个地方,无济于事。




-----------------

I'm very new to Unity and C# programming, so please keep answers simple.

In short : Game does not respond to state buttons. Imported audio file throws up an error, console error messages mystifying me.

INFO : Unity version 4.6.9F1


Thank you!

What I have tried:

Fudging around with code, a few UI interaction changes, dragging script asset file to various places, to no avail.

推荐答案

来自注释,我们发现代码中名为text的Text对象未初始化。由于您将其声明为Public,因此它将在主Unity窗口的Script属性中显示为项目。将Text对象从层次结构中的Canvas拖放到脚本中的Text属性中。
From the comments, we found that your Text object named "text" in the code was not initialized. Since you declared it as Public it will show as an item in the Script properties in the main Unity Window. Drag and drop your Text object from the Canvas in the hierarchy into the Text property in the script.


这篇关于[已解决]如何根据状态更改画布上的文本。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆