[HELP] Why will my script not run?

While attempting to solve a bug that reset the guess to that was used in the previous game, the entire program will now not even start the C# Script.

This is very frustrating as I have tried to solve this problem by redoing this entire segment of the course twice and am still having the same problem at this stage.

I will add the entire code, as I do not understand which bit is causing this issue (I am very new to coding). So my question has two parts:

  1. Why will my C# Script not run at all anymore, as in the console is displaying absolutely nothing?

  2. Why (before the script didn’t run) does my ‘Guess’ reset to the guess that it was in the previous game when attempting a second game?

Many thanks.

using UnityEngine;
using System.Collections;

public class NumberWizard : MonoBehaviour {

// Use this for initialization
int max;
int min;
int guess;

void Start () {
	StartGame();
}

void StartGame () {
	max = 1000;
	min = 1;
	guess = 500;
	
	max = max + 1;
	
	print ("Welcome to Number Wizard");
	print ("Pick a number in your head");
	print ("But don't tell me yet!");
	
	print ("The highest number you can pick is " + max);
	print ("The lowest number you can pick is " + min);
	
	print ("Is the number higher or lower than " + guess + "?");
	print ("Press up if higher, press down if lower and press Enter if equals");
	
}

// Update is called once per frame
void Update () {
	if (Input.GetKeyDown (KeyCode.UpArrow)) {
		min = guess;
		NextGuess();
	} else if (Input.GetKeyDown (KeyCode.DownArrow)) {
		max = guess;
		NextGuess();
	} else if (Input.GetKeyDown (KeyCode.Return)) {
		print("I win! The game is over!");
		print ("=========================================");
		max = 1000;
		min = 1;
		guess = 500;
		StartGame();
	}
	
}

void NextGuess () {
	guess = (max + min) / 2;
	print("My new guess is " + guess + ". Is your number higher or lower than " + guess + "?");
	print("Test");
	print("Up is Higher! Down is Lower! Enter is Equals!");
}

}

Hello there, how are you @And_Then?

Are you sure that this script is actually in the game scene?

Take a look at the component within the gameobject in the scene to see if didn’t lost the reference to this script

The same exact thing happened to me. My script was running fine within the Console, until it didn’t. I have no idea what caused it, but here is what I did to correct it.

Be sure to copy your script into notepad.

A script must be attached to an object.

Be sure there is an object in the Hierarchy panel. If not, right click in the panel and “Create Empty”.

Click on the Inspector panel. Drag your existing C# script from the Assets panel into the Inspector panel.

If your script still does not run in Console, delete the existing object and script. After you create a new object and script be sure to attach your script as outlined above.

Then paste your code from notepad into Monodevelop, save, and play.

Hopefully, this solves your problem.

Do you mean that every game start with the same guess?
That is because in your StartGame() method you set guess to 500. So every time you run this method (which is at the start) this will happens.

As the game is not working and you are not getting any errors in the console, I suspect you have accidentally turned off the logs the console is showing, so the game may be running you are just not seeing what the console prints.

To turn it back on press the button on the top right of the console.

First, when you say the Guess resets, do you mean to the starting guess from the previous game, or that is has the last guess made before the game ended. If it’s the former, then like Voetst said, it’s because “guess” is set to 500, so the first guess will always be the same. In another video you will learn how to randomize it.

I also noticed that in the Update function, after you press Enter, you set the value of “max,” “min” and “guess,” then run StartGame and set them again. That is redundant code and you can go ahead and delete the three lines in Update.

Also for the script not working, try what has been suggested above, and if that still isn’t working, make sure the object the script is attached to is active, meaning that in the Inspector, to the left of the name, make sure the box is ticked. If it still isn’t working after that, copy the code and start a new project.

Hope you get it working again!