- Home /
Text Adventure
My last post was rejected and i did not get an answer on why so i test again.
Hi i'm learning c# and do so by making an old school text adventure. my problem is i cant figure out one simple thing: i got one c# file that starts the game and make you make some questions up till one point where you make a big decision and change the story forever! it's like one answer you go do that an then all this happens and the other answer this stuff happens and because the story is splitting in two ways here i want two separate files whit the with the continuation of the story. here is the last part of my "first" part of the story and game:
void out_1()
{
text.text = "You're out of your house, and you must decide. \n" +
"Take the Car or the Dirt bike?" +
"\n\n" +
"C to take the Car, D to take the Dirt bike";
if (Input.GetKeyDown(KeyCode.C)) { myState = States.car; }
else if (Input.GetKeyDown(KeyCode.D)) { myState = States.dirtBike; }
}
so basically i want:
myState = States.car; }
go to one c# file
and
myState = States.dirtBike; }
to an other c# file
how can i make this happen?
What do you mean go to one file and then got to another file? Are they same type? Are they Component?
public class TheScript:$$anonymous$$onoBehaviour{
public States state;
}
Questions like this often lead to very long back and forths on basics of implementation. Please use the "Learn" button at the top and use the resources to get some experience with code. There are many examples of how to call methods in one class from another (which would be your "go to one c# file").
Answer by Positive7 · Aug 18, 2015 at 04:27 PM
Main class:
using UnityEngine;
using UnityEngine.UI;
public class test : MonoBehaviour {
public enum States
{
car,
dirtBike,
None
}
public test.States myState;
Text text;
bool _choice = true;
void Awake(){
myState = test.States.None;
}
void Start()
{
text = GetComponent<Text>();
text.text = "You're out of your house, and you must decide. \n" +
"Take the Car or the Dirt bike?" +
"\n\n" +
"C to take the Car, D to take the Dirt bike";
}
void Update(){
if (Input.GetKeyDown(KeyCode.C) && _choice){
myState = test.States.car;
_choice = false;
}
else if (Input.GetKeyDown(KeyCode.D) && _choice){
myState = test.States.dirtBike;
_choice = false;
}
}
}
Car Class:
using UnityEngine;
public class Car : MonoBehaviour {
public test _test; //assign it in Inspector or Initialize it in Awake() / Start()
bool _active = true; //so it won't get called every frame after it's false
void Update () {
if(_test.myState == test.States.car && _active){
Debug.Log("Go with Car");
_active = false;
}
}
}
DirtBike class :
using UnityEngine;
public class DirtBike : MonoBehaviour {
public test _test; //assign it in Inspector or Initialize it in Awake() / Start()
bool _active = true;
void Update () {
if(_test.myState == test.States.dirtBike && _active){
Debug.Log("Go with DirtBike");
_active = false;
}
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How can I match and display two random numbers using C#? 2 Answers
UI text color not assigning correctly 2 Answers
Text UI broken after changing alpha color of single letters 1 Answer