Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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;}
	}



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

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.
Posted
Updated 17-May-17 4:49am
v3
Comments
ZurdoDev 17-May-17 10:23am    
The error says that something is null on line 81.
Chill Naga 17-May-17 10:28am    
Line 81 is the first "text.text" instance - the one you can see in my code snippet up above. In the bottom left of Unity proper, I don't see a red warning for that, I see a an olive colored notification because I accidentally called one of my state "light" and it simply tells me there is a function with that name.
ZurdoDev 17-May-17 10:31am    
You define text as a Text object at the top of your class but then you never set it anywhere in the code. Do you set it in the designer?
Chill Naga 17-May-17 10:33am    
I'm sorry, not sure what you mean setting it in the designer. You mean the main Unity window? I do think that's what I've forgotten how to do >.<. One screenshot involves my highlighting a "text" element that shows I copied game.cs - or the script with the text strings in it - into a place that accepted it.
ZurdoDev 17-May-17 10:35am    
Yes, so first off, just make sure you understand the error. It is saying that text has not been initialized so you can't call text.text.

Second, since you declared your variable as Public it should show in the properties window of the script in the main Unity window. You can drag and drop your Text object from the Canvas to it or you can do the same thing in code.

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900