Code added to script is not executing at runtime

Hello.
I added the code:
print (“Welcome to Number Wizard”);
to the NumberWizard.cs script. When I hit the PLAY button in Unity, the code does not run in the console.

Hi Paolo,

Is your NumberWizard.cs script attached to a GameObject in the Hierarchy?

Hi Rob.
How do I check that?

Hi,

Well, did you add the script as a component to anything in the Hierarchy? Either by dragging it onto a GameObject, or by using the Add Component button within the Inspector, for a selected GameObject in the Hierarchy?

If you are not sure, simply select each of your GameObjects in the Hierarchy one at a time, and review the components in the Inspector, usually on the right-hand side of the screen. If you do not see a script component named Number Wizard (script) then you can assume that you haven’t added it yet.

In order for your code to be executed, where it inherits from MonoBehaviour (if you just created the script within Unity then it will be default), it needs to be added to a GameObject which is active in your scene.

If it isn’t there, then create a new empty GameObject in the Hierarchy, and name it NumberWizard, then use either of the methods mentioned above to add your script to it. Now run the game and you should see your text appear in the console.

Let me know if you are still having difficulties.

Thanks. I fixed it. Thanks for your quick reply!

1 Like

Awesome!

You are very welcome, I am glad you can move forward with the course again :slight_smile:

Now I added a secont print line:

	void Start () {
		print ("Welcome to Nuimber Wizard.");
		print ("Pick a number in your head, but don't tell me.");
	}

The first print line prints, but the second print line does not print.

When you make the changes to the script and click back into Unity, you should see a small spinning circle in the bottom right-hand corner of the screen as the script is updated within Unity.

Select your script in the Project View, then, in the Inspector you will see all of your lines of code. Is the second print statement displayed?

Note, if you still have your game running within Unity you will need to stop it before the changes you have made will take effect.


See also;

My revised code appears in the Inspector.

…and you stopped the game from running and then re-started it after making the changes?

Yes.
My code:

	void Start () {
		print ("Welcome to Nuimber Wizard.");
		print ("Pick a number in your head, but don't tell me.");
		int max = 1000;
		int min = 1;
		print ("The highest number you can pick is " + max);
		print ("The lowest number you can pick is " + min);
	}

Can you pop a screenshot up for me please, with the game running.


See also;

Here’s the screen shot:

Hi,

I see this script is now named NewBehaviourScript.cs, did you start again? If so, have you attached this script to the NumberWizard GameObject?

If I had to make a guess here I would say that perhaps your previous script is still attached to the NumberWizard GameObject, and you have created a new script named NewBehaviourScript.cs and placed it within the Assets / Scripts directory.

When your game runs, it is running the script which is still attached, not this new script.

Follow the steps at the top of this topic again to check what is attached to the NumberWizard GameObject. If I am correct, you can simply copy/paste the code from the new script into the existing script (probably in the Assets folder). Just make sure that you then change the name of the class back to NumberWizard from NewBehaviourScript.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms