im new to Unity and wanted to try to make a menu were you need to answer a question before you start.
i tryed to make it over the 3rdPersonShooter example project.
if any of you can help me i would be warry happy…
i have postet the MenuController.cs under here
using UnityEngine;
using System.Data;
using System.IO;
using System.Collections;
using System.Collections.Generic;
public class MenuController : MonoBehaviour {
static int difficulty = 1;
public GUISkin skin;
private List<Animation> animationComponents;
private List<AudioSource> audioSourceComponents;
private float menuOn = 0;
private float lastTime = 0;
int[] playerHitDamage = new int[5] { 3, 4, 6, 10, 20 };
int[] playerHeal = new int[5] { 5, 4, 3, 2, 0 };
int[] enemyHitDamage = new int[5] { 10, 5, 2, 2, 2 };
int[] enemyHeal = new int[5] { 0, 0, 0, 0, 0 };
// Use this for initialization
IEnumerator Start () {
UpdateDifficulty();
AudioListener.volume = 0;
yield return 0;
AudioListener.volume = 1;
}
// Update is called once per frame
void Update () {
// Don't pause in first frame - allow scripts to settle in first
if (Time.timeSinceLevelLoad == 0)
return;
float realDeltaTime = (Time.realtimeSinceStartup - lastTime);
lastTime = Time.realtimeSinceStartup;
menuOn = Mathf.Clamp01(menuOn + (Time.timeScale == 0 ? 1 : -1) * realDeltaTime * 5);
if (!Screen.lockCursor Time.timeScale != 0) {
StartCoroutine(Pause(true));
}
}
void OnGUI () {
if (menuOn == 0)
return;
GUI.skin = skin;
string question = "";
string answer1 = "";
string answer2 = "";
string answer3 = "";
string ranswer = "";
bool ranswer2 = false;
string line;
StreamReader file = new StreamReader("database.txt");
while ((line = file.ReadLine()) != null)
{
if (line.Contains("opg" + (Random.Range(1, 50)) + ","))
{
string[] output = line.Split(',');
foreach (string Builder in output)
{
if (Builder == "question")
{
question = Builder;
}
if (Builder == "answer1")
{
answer1 = Builder;
}
if (Builder == "answer2")
{
answer2 = Builder;
}
if (Builder == "answer3")
{
answer3 = Builder;
}
if (Builder == "ranswer")
{
ranswer = Builder;
}
}
}
}
file.Close();
Rect rect1 = new Rect(0, 0, 150, 75);
rect1.x = (Screen.width - rect.width ) / 2 + (1 - menuOn) * Screen.width;
rect1.y = (Screen.height - rect.height) / 2;
// Make the buttons.
if (GUI.Button (rec1t, answer1)) {
if (answer1 == ranswer)
{
ranswer2 = true;
}
}
Rect rect2 = new Rect(1, 0, 150, 75);
rect2.x = (Screen.width - rect.width ) / 2 + (1 - menuOn) * Screen.width;
rect2.y = (Screen.height - rect.height) / 2;
if (GUI.Button (rect2, answer2)) {
if (answer2 == ranswer)
{
ranswer2 = true;
}
}
Rect rect3 = new Rect(2, 0, 150, 75);
rect3.x = (Screen.width - rect.width ) / 2 + (1 - menuOn) * Screen.width;
rect3.y = (Screen.height - rect.height) / 2;
if (GUI.Button (rect3, answer3)) {
if (answer3 == ranswer)
{
ranswer2 = true;
}
}
if (ranswer2 == true)
{
Rect rect = new Rect(0, 0, 150, 75);
rect.x = (Screen.width - rect.width ) / 2 + (1 - menuOn) * Screen.width;
rect.y = (Screen.height - rect.height) / 2;
if (GUI.Button(rect, "PLAY")) {
// PLAY button
if (GUI.Button(rect, "PLAY")) {
StartCoroutine(Pause(false));
}
// Difficulty buttons
rect = new Rect(rect.x - 200, rect.y + 150, rect.width + 400, 40);
string[] difficulties = new string[] {"NO FAIL", "EASY", "MEDIUM", "HARD", "INSANE"};
int newDifficulty = GUI.SelectionGrid(rect, difficulty, difficulties, difficulties.Length);
if (newDifficulty != difficulty) {
difficulty = newDifficulty;
UpdateDifficulty();
}
}
}
IEnumerator Pause (bool pause) {
// Pause/unpause time
Time.timeScale = (pause ? 0 : 1);
// Unlock/Lock cursor
Screen.lockCursor = !pause;
if (pause == true) {
Object[] objects = FindObjectsOfType(typeof(Animation));
animationComponents = new List<Animation>();
foreach (Object obj in objects) {
Animation anim = (Animation)obj;
if (anim != null anim.enabled) {
animationComponents.Add(anim);
anim.enabled = false;
}
}
objects = FindObjectsOfType(typeof(AudioSource));
audioSourceComponents = new List<AudioSource>();
foreach (Object obj in objects) {
AudioSource source = (AudioSource)obj;
if (source != null source.enabled /* source.isPlaying*/) {
audioSourceComponents.Add(source);
source.Pause();
}
}
}
else {
// If unpausing, wait one frame before we enable animation component.
// Procedural adjustments are one frame delayed because first frame
// after being paused has deltaTime of 0.
yield return 0;
foreach (Animation anim in animationComponents)
anim.enabled = true;
foreach (AudioSource source in audioSourceComponents)
source.Play();
animationComponents = null;
}
}
void UpdateDifficulty () {
Object[] objects = FindObjectsOfType(typeof(HealthController));
foreach (Object obj in objects) {
HealthController health = (HealthController)obj;
if (health.gameObject.tag == "Player") {
health.healingSpeed = playerHeal[difficulty];
health.hitDamage = playerHitDamage[difficulty];
}
else {
health.healingSpeed = enemyHeal[difficulty];
health.hitDamage = enemyHitDamage[difficulty];
}
}
}
}