GetAxis is not allowed to be called from a MonoBehaviour constructor

edit: I tried this in Unity 2022.3.1 and it works perfectly so this issue might be version specific.


Hi there, I tried following the tutorial but Unity doesn’t seem to be happy with GetAxis being in Update(). Moving it to Start() like it suggests causes the variables to be “out of context”.

Is this an issue with Unity 2023.2.1? I’ve attached a screenshot if it helps!

Thank you :slight_smile:

The code doesn’t seem to match the error. The error says you may not call Input.GetAxis() in the constructor of a MonoBehaviour but there’s nowhere in the code where you do. Can you show the console window with the full error?

Ah I’m so sorry I uninstalled Unity 2023.2.1 and successfully redid the tutorial in 2022.3.1 so I don’t have access to the error message anymore!

I do believe it ended among the lines of:

Called from MonoBehaviour ‘player’ on game object ‘Player (Monki)’. See “Script Serialization” page in the Unity Manual for further details.

Could it be that 2023 Unity just handles Input.GetAxis() in a different way?

(Also no prizes for why I named the sphere Monki)

No, the message and the code don’t match. Update is not the constructor nor instance field initialization., and you are using it exactly like anyone else would be. Perhaps - and I do this often so are aware of it - you just saved the script while you were still writing the code and Unity started compiling while the code was in an incomplete state.

I see—earlier on I did try to assign Input.GetAxis() to a variable before void Start() [I assume that bit is the constructor?] but Unity wasn’t happy so I pressed undo.

That’s probably what it was—thank you so much!

Yes, that’s invalid code. Normal methods can only be called within methods. It is very likely that this invalid version of your class remained in Unity’s cache. Start() is not a constructor but a normal method. Unity does not allow us to use constructors of classes inheriting from Unity classes such as MonoBehaviour. Unity calls the constructor itself, and we do not know where it creates the objects exactly.

For future reference, it would be great if you could also share information on what you did before a problem occured, especially if an error was involved. Sometimes, unexpected things are going on in Unity, so sharing a bit more context, especially when it comes to errors that don’t make any sense, can help. :slight_smile:

I ran into the same issue.

Unity appears to be getting confused with all changes to the script we’re doing in the tutorial. Even though the script is calling for GetAxis in the correct place, Unity is certain you’re calling for it in a MonoBehaviour Constructor (whatever that means).

People are rightly saying that’s not an error you should expect, because it’s a bug.

I fixed the issue by deleting the script from the project entirely in the assets tab and removing it from my Player object (Dodgy in Rick’s game), then remaking the script and pasting the code over, and reattaching it to the Player object. Remember to copy the code to your clipboard before you delete the c# script unless you’re confident you can just type it all out again.

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

Privacy & Terms