0

I am testing simple variables and methods in unity with a script i have attached to the main camera called "LearningScript". I programmed a method called "AddTwoNumbers" to add the sum of two predetermined variables and output the result in the console. I then used the Input.GetKeyUp method to call the "AddTwoNumbers" method whenever input from the return key is given. When it compiles the script and i go to test it out, nothing appears in the console. I have no idea what i did wrong and can't find the mistake.

using System.Collections; using System.Collections.Generic; using UnityEngine; public class LearningScript : MonoBehaviour { public int number1 = 1; public int number2 = 9; // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (Input.GetKeyUp(KeyCode.Return)) AddTwoNumbers(); } void AddTwoNumbers() { Debug.Log(number1 + number2); } }

1 Answer 1

1

Your code looks fine, there are a couple of likely culprits I'd suggest checking out:


  1. Check your messages aren't being filtered by the console.

Unity Console

See the three highlighted icons in the top left of the console window? You can click these to toggle if that type of message is shown. Ensure the info icon (left-most speech bubble) is highlighted!


  1. Check Learning Script has been attached to a GameObject in your active scene.

Pretty self explanatory but it can be easy to forget. Confirm you have attached it by finding it in your hierarchy before pressing Play. You also want to ensure that the GameObject is active otherwise Update will not be called.

Sign up to request clarification or add additional context in comments.

1 Comment

It turns out i wasn't on the game screen while pressing enter, I was on the console screen while pressing it, which explains my problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.