M
maditrumble
Guest
Hi! So I'm working on a school project and have been asking my professor questions, but he is not very kind or willing to help so I'm turning here. I am trying to have a welcome page on my text adventure game. No matter what, the first page to come up when I hit play on unity is the second page. Stranger still, I have it set to ask if you want to play again when you die, and when you hit y, it is routed to the welcome page, yet the second page is what comes up. In the original game, there was no welcome page. From what I can see, I have changed all previous coding to indicate the new page 1, but my game just will not show it. I copy and pasted the whole code here, because I have no idea where in the code it is getting confused. Sorry this is a lot to ask, but I am just curious as to why I cannot get it to work. To make things more clear page 1 is meant to be welcome, and page 2 is the start of the story, "wake up".
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TrumbleMadison_Week2Project : MonoBehaviour
{
Text screen;
Page[] book;
[SerializeField]
string prevHeading;
[SerializeField]
string currHeading;
[SerializeField]
string nextHeading = "welcome!";
// Start is called before the first frame update
void Start()
{
GameObject go = GameObject.Find("MainText");
if (go)
{
screen = go.GetComponent<Text>();
if (!screen)
{
Debug.LogError("Text Component was not found on Maintext");
}
}
else
{
Debug.LogError("MainText not found");
}
//screen.text = "Hello World";
Bindbook();
}
// Update is called once per frame
void Update()
{
HandleInput();
RenderStory();
}
void Bindbook()
{
book = new Page[]
{
//13
new Page("welcome!", "<i>(this is: welcome)</i>\n\nwelcome to my game! please press <b><color=blue>[space]</color></b> to continue"),
new Page("wake up", "<i>(this is: wake up)</i>\n\nyou awaken in a sweltering room that smells of dust and dry rot. to your left, you see a woven basket, and about three feet in front of you, there is a deep steel bowl filled with what looks like milk. ten feet beyond that is a heavy iron door you assume is locked. will you check the <color=blue><b>[w]</b></color>oven basket, <color=blue><b></b></color>owl, or <color=blue><b>[d]</b></color>oor?"),
new Page("basket", "<i>(this is: basket)</i>\n\nyou walk over to examine the woven basket, which stands almost 3 feet tall. you see that the lid on the basket has a handle, but when you try to pull up, it seems to be tied down. you find ties on four sides of the basket holding the lid on. you undo the ties and find the basket oddly empty. however, you notice the stone beneath it is loose. do you <color=blue><b></b></color>nspect the stone, or press <color=blue><b>[x]</b></color> to return to the previous step."),
new Page("inspect", "<i>(this is: inspect)</i>\n\nyou reach between the stones and manage to pry it up. it reveals a narrow tunnel leading down, but you can't see too far into it because it is dark. do you go <color=blue><b>[d]</b></color>own into the tunnel, or look for a <color=blue><b>[l]</b></color>ight source to get a better view?"),
//lose
new Page("down", "<i>(this is: down)</i>\n\nyou head down into the tunnel despite not knowing what else may be there. you stumble around for a moment before falling straight down a booby trap that you surely would have seen with a light. <b><color=red>you died.</color></b>would you like to try again?\n\n<color=blue><b>[y]</b></color> or <color=blue><b>[n]</b></color>"),
new Page("light", "<i>(this is: light)</i>\n\nyou search the area for any possible source of light and discover a flashlight. you turn it on, and it flickers, but you can see a bit further into the tunnel. would you like to go into the <color=blue><b>[t]</b></color>unnel, or check out the <color=blue><b></b></color>owl of milk you noticed earlier"),
new Page("tunnel", "<i>this is: tunnel)</i>\n\nyou use your light to venture into the tunnel, noticing a large trap hole in the center of the pathway. you side step it and continue onward. you may either continue <color=blue><b></b></color>traight or <color=blue><b>[r]</b></color>eturn to the surface."),
//win!
new Page("straight", "<i>(this is: straight)</i>\n\nyou continue straight and, to your surprise, it comes up to an exit near a desert resort. you check in. <b><color=lime>you have a great time and survive!</color><b>would you like to play again?\n\n<color=blue><b>[y]</b></color> or <color=blue><b>[n]</b></color>"),
new Page("door", "<i>(this is: door)</i>\n\nyou think it should be easy enough to waltz through an iron door. when you come upon it, you realize it is much heavier than originally thought. you put all your weight on it, but it only groans and will not budge.\n\nit seems you will need a key to unlock the door before it moves anymore. press <color=blue><b>[x]</b></color> to return to the previous step."),
new Page("bowl of milk", "<i>(this is: bowl of milk)</i>\n\nyou look at the bowl of creamy liquid before you, unsure of its contents. you tap the bowl with your foot and see that the liquid moves with the same viscosity as milk would. you are quite parched. do you <color=blue><b>[p]</b></color>ick up the bowl, or press <color=blue><b>[x]</b></color> to return?"),
new Page("pick up", "<i>(this is: pick up)</i>\n\nyou pick up the bowl and bring it closer to sniff the contents. the smell of the liquid is mild, although there is a twinge of something odd to it that you cannot place your finger on. you slosh the liquid a bit and notice something shifting at the bottom of the bowl. do you <color=blue><b>[p]</b></color>our out the contents or <color=blue><b>[d]</b></color>rink the liquid? or press <color=blue><b>[x]</b></color> to return to the previous step."),
//win!
new Page("pour", "<i>(this is: pour)</i>\n\nyou pour out all the contents onto the dusty floor, including what seems to be some maggots and a wrought iron key. one that looks to fit in the keyhole of the heavy door. you use to unlatch the door and budge it enough to escape. <color=lime><b>you win!</b></color> would you like to play again?\n\n<color=blue><b>[y]</b></color> or <color=blue><b>[n]</b></color>"),
//lose
new Page("drink", "<i>(this is: drink)</i>\n\nyou decide your thirst could help to kill two birds with one stone and begin to drink the liquid. the liquid isn't milk at all and has a severe acrid taste. you swallow some more anyways, feeling chunks of items floating on your tongue. you spit out the bits and upon further inspection see they are wriggling. you toss the bowl away from you as you feel the need to wretch. standing up, you see a key laying next to the tossed bowl. as you go to reach for it, you fall forward, mind hazing into unconciousness. <b><color=red>you died.</color></b> would you like to try again?\n\n<color=blue><b>[y]</b></color> or <color=blue><b>[n]</b></color>"),
new Page("thanks", "<i>(this is: thanks for playing!</i>\n\n thank you for playing!")
};
}
void RenderStory()
{
if (!string.IsNullOrEmpty(nextHeading))
{
for (int i = 0; i < book.Length; i++)
{
if (nextHeading == book.Heading)
{
prevHeading = currHeading;
currHeading = nextHeading;
nextHeading = "";
screen.text = book.Body;
Debug.Log(book.Body);
return;
}
}
Debug.LogWarning("Heading not found: \"" + nextHeading + "\"");
}
}
void HandleInput()
{
if (currHeading == "welcome!")
{
nextHeading = "wake up";
}
if (Input.GetKeyDown(KeyCode.Space))
{
if (currHeading == "welcome!")
{
nextHeading = "wake up";
}
}
if (Input.GetKeyDown(KeyCode.W))
{
if (currHeading == "wake up")
{
nextHeading = "basket";
}
}
else if (Input.GetKeyDown(KeyCode.B))
{
if (currHeading == "wake up" || currHeading == "light")
{
nextHeading = "bowl of milk";
}
}
else if (Input.GetKeyDown(KeyCode.D))
{
if (currHeading == "wake up")
{
nextHeading = "door";
}
else if (currHeading == "inspect")
{
nextHeading = "down";
}
else if (currHeading == "pick up")
{
nextHeading = "drink";
}
}
else if (Input.GetKeyDown(KeyCode.I))
{
if (currHeading == "basket")
{
nextHeading = "inspect";
}
}
else if (Input.GetKeyDown(KeyCode.L))
{
if (currHeading == "inspect")
{
nextHeading = "light";
}
}
else if (Input.GetKeyDown(KeyCode.T))
{
if (currHeading == "light")
{
nextHeading = "tunnel";
}
}
else if (Input.GetKeyDown(KeyCode.X))
{
nextHeading = prevHeading;
}
else if (Input.GetKeyDown(KeyCode.Y))
{
if (currHeading == "down" || currHeading == "straight" || currHeading == "pour" || currHeading == "drink")
{
nextHeading = "welcome!";
}
}
else if (Input.GetKeyDown(KeyCode.P))
{
if (currHeading == "bowl of milk")
{
nextHeading = "pick up";
}
else if (currHeading == "pick up")
{
nextHeading = "pour";
}
}
else if (Input.GetKeyDown(KeyCode.N))
{
if (currHeading == "drink" || currHeading == "pour" || currHeading == "straight" || currHeading == "down")
{
nextHeading = "thanks";
}
}
}
}
Continue reading...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TrumbleMadison_Week2Project : MonoBehaviour
{
Text screen;
Page[] book;
[SerializeField]
string prevHeading;
[SerializeField]
string currHeading;
[SerializeField]
string nextHeading = "welcome!";
// Start is called before the first frame update
void Start()
{
GameObject go = GameObject.Find("MainText");
if (go)
{
screen = go.GetComponent<Text>();
if (!screen)
{
Debug.LogError("Text Component was not found on Maintext");
}
}
else
{
Debug.LogError("MainText not found");
}
//screen.text = "Hello World";
Bindbook();
}
// Update is called once per frame
void Update()
{
HandleInput();
RenderStory();
}
void Bindbook()
{
book = new Page[]
{
//13
new Page("welcome!", "<i>(this is: welcome)</i>\n\nwelcome to my game! please press <b><color=blue>[space]</color></b> to continue"),
new Page("wake up", "<i>(this is: wake up)</i>\n\nyou awaken in a sweltering room that smells of dust and dry rot. to your left, you see a woven basket, and about three feet in front of you, there is a deep steel bowl filled with what looks like milk. ten feet beyond that is a heavy iron door you assume is locked. will you check the <color=blue><b>[w]</b></color>oven basket, <color=blue><b></b></color>owl, or <color=blue><b>[d]</b></color>oor?"),
new Page("basket", "<i>(this is: basket)</i>\n\nyou walk over to examine the woven basket, which stands almost 3 feet tall. you see that the lid on the basket has a handle, but when you try to pull up, it seems to be tied down. you find ties on four sides of the basket holding the lid on. you undo the ties and find the basket oddly empty. however, you notice the stone beneath it is loose. do you <color=blue><b></b></color>nspect the stone, or press <color=blue><b>[x]</b></color> to return to the previous step."),
new Page("inspect", "<i>(this is: inspect)</i>\n\nyou reach between the stones and manage to pry it up. it reveals a narrow tunnel leading down, but you can't see too far into it because it is dark. do you go <color=blue><b>[d]</b></color>own into the tunnel, or look for a <color=blue><b>[l]</b></color>ight source to get a better view?"),
//lose
new Page("down", "<i>(this is: down)</i>\n\nyou head down into the tunnel despite not knowing what else may be there. you stumble around for a moment before falling straight down a booby trap that you surely would have seen with a light. <b><color=red>you died.</color></b>would you like to try again?\n\n<color=blue><b>[y]</b></color> or <color=blue><b>[n]</b></color>"),
new Page("light", "<i>(this is: light)</i>\n\nyou search the area for any possible source of light and discover a flashlight. you turn it on, and it flickers, but you can see a bit further into the tunnel. would you like to go into the <color=blue><b>[t]</b></color>unnel, or check out the <color=blue><b></b></color>owl of milk you noticed earlier"),
new Page("tunnel", "<i>this is: tunnel)</i>\n\nyou use your light to venture into the tunnel, noticing a large trap hole in the center of the pathway. you side step it and continue onward. you may either continue <color=blue><b>
//win!
new Page("straight", "<i>(this is: straight)</i>\n\nyou continue straight and, to your surprise, it comes up to an exit near a desert resort. you check in. <b><color=lime>you have a great time and survive!</color><b>would you like to play again?\n\n<color=blue><b>[y]</b></color> or <color=blue><b>[n]</b></color>"),
new Page("door", "<i>(this is: door)</i>\n\nyou think it should be easy enough to waltz through an iron door. when you come upon it, you realize it is much heavier than originally thought. you put all your weight on it, but it only groans and will not budge.\n\nit seems you will need a key to unlock the door before it moves anymore. press <color=blue><b>[x]</b></color> to return to the previous step."),
new Page("bowl of milk", "<i>(this is: bowl of milk)</i>\n\nyou look at the bowl of creamy liquid before you, unsure of its contents. you tap the bowl with your foot and see that the liquid moves with the same viscosity as milk would. you are quite parched. do you <color=blue><b>[p]</b></color>ick up the bowl, or press <color=blue><b>[x]</b></color> to return?"),
new Page("pick up", "<i>(this is: pick up)</i>\n\nyou pick up the bowl and bring it closer to sniff the contents. the smell of the liquid is mild, although there is a twinge of something odd to it that you cannot place your finger on. you slosh the liquid a bit and notice something shifting at the bottom of the bowl. do you <color=blue><b>[p]</b></color>our out the contents or <color=blue><b>[d]</b></color>rink the liquid? or press <color=blue><b>[x]</b></color> to return to the previous step."),
//win!
new Page("pour", "<i>(this is: pour)</i>\n\nyou pour out all the contents onto the dusty floor, including what seems to be some maggots and a wrought iron key. one that looks to fit in the keyhole of the heavy door. you use to unlatch the door and budge it enough to escape. <color=lime><b>you win!</b></color> would you like to play again?\n\n<color=blue><b>[y]</b></color> or <color=blue><b>[n]</b></color>"),
//lose
new Page("drink", "<i>(this is: drink)</i>\n\nyou decide your thirst could help to kill two birds with one stone and begin to drink the liquid. the liquid isn't milk at all and has a severe acrid taste. you swallow some more anyways, feeling chunks of items floating on your tongue. you spit out the bits and upon further inspection see they are wriggling. you toss the bowl away from you as you feel the need to wretch. standing up, you see a key laying next to the tossed bowl. as you go to reach for it, you fall forward, mind hazing into unconciousness. <b><color=red>you died.</color></b> would you like to try again?\n\n<color=blue><b>[y]</b></color> or <color=blue><b>[n]</b></color>"),
new Page("thanks", "<i>(this is: thanks for playing!</i>\n\n thank you for playing!")
};
}
void RenderStory()
{
if (!string.IsNullOrEmpty(nextHeading))
{
for (int i = 0; i < book.Length; i++)
{
if (nextHeading == book.Heading)
{
prevHeading = currHeading;
currHeading = nextHeading;
nextHeading = "";
screen.text = book.Body;
Debug.Log(book.Body);
return;
}
}
Debug.LogWarning("Heading not found: \"" + nextHeading + "\"");
}
}
void HandleInput()
{
if (currHeading == "welcome!")
{
nextHeading = "wake up";
}
if (Input.GetKeyDown(KeyCode.Space))
{
if (currHeading == "welcome!")
{
nextHeading = "wake up";
}
}
if (Input.GetKeyDown(KeyCode.W))
{
if (currHeading == "wake up")
{
nextHeading = "basket";
}
}
else if (Input.GetKeyDown(KeyCode.B))
{
if (currHeading == "wake up" || currHeading == "light")
{
nextHeading = "bowl of milk";
}
}
else if (Input.GetKeyDown(KeyCode.D))
{
if (currHeading == "wake up")
{
nextHeading = "door";
}
else if (currHeading == "inspect")
{
nextHeading = "down";
}
else if (currHeading == "pick up")
{
nextHeading = "drink";
}
}
else if (Input.GetKeyDown(KeyCode.I))
{
if (currHeading == "basket")
{
nextHeading = "inspect";
}
}
else if (Input.GetKeyDown(KeyCode.L))
{
if (currHeading == "inspect")
{
nextHeading = "light";
}
}
else if (Input.GetKeyDown(KeyCode.T))
{
if (currHeading == "light")
{
nextHeading = "tunnel";
}
}
else if (Input.GetKeyDown(KeyCode.X))
{
nextHeading = prevHeading;
}
else if (Input.GetKeyDown(KeyCode.Y))
{
if (currHeading == "down" || currHeading == "straight" || currHeading == "pour" || currHeading == "drink")
{
nextHeading = "welcome!";
}
}
else if (Input.GetKeyDown(KeyCode.P))
{
if (currHeading == "bowl of milk")
{
nextHeading = "pick up";
}
else if (currHeading == "pick up")
{
nextHeading = "pour";
}
}
else if (Input.GetKeyDown(KeyCode.N))
{
if (currHeading == "drink" || currHeading == "pour" || currHeading == "straight" || currHeading == "down")
{
nextHeading = "thanks";
}
}
}
}
Continue reading...