Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

fedefaran

Hi. I´m doing the Multiplayer FPS tutorial. I followed step by step the 2 first tutorials about player movement and rotation but Unity throws me this errors. I´m using Unity 2019.4.10 and Visual Studio 2019. Help please. I´m a total rookie!


Best Answer

  • edited October 2020 Accepted Answer

    Its because your player motor is not being set properly in the awake function this is probably due to the Player Motor not being on the same object as the movement script. A good way to think of get component is that the script will check what other components its parent has so for example lets say you are getting script1 from script2 and lets say your hierarchy on that object looks like this:

    Cube

    -transform

    -box collider

    -script1

    -script2

    -Rigidbody

    First script2 is going to start on transform and it will see transform is not of type script1 and this process will repeat until it gets to script1 where it will set its script1 field to that instance of script1. However if there is no script 1 attached to its parent it will come back as null.

    Keep in mind this is not like an int where it is set to 0 if it is not set to anything else, this is set to nothing meaning anything called on it will result in an error but since null is still a valid type this wont result in an error directly where you are trying to get the component.

    There are a few solutions to this problem 1. Serialize it and set it in the inspector(takes up slightly more performance on start or awake but if you are loading your scene from a menu asynchronously it shouldn't matter)2. Use another type of get component such as GetComponentInChildren. here is the documentation on components: https://docs.unity3d.com/ScriptReference/Component.html 3. get a gameobject with tag: GameObject go = GameObject.FindGameObjectWithTag("tag”); then you can use go.GetComponet(); This takes up alot of performance and should be done as little as possible(run in start or awake with preferably loading your scene from a menu asynchronously). The only use case I've ever found for this is with mirror networking manipulating objects that you dont want to be included in your network transform so cant have in your player prefab. I would certainly recommend 2 when possible then 1 as a close second with 3 being a last resort.

    TLDR:

    NullRefrenceExeption just means a method is being called on a null reference of a component.

    Go to link and look into other types of Get Component or serialize the field.

    https://docs.unity3d.com/ScriptReference/Component.html

Answers

  • Can you please send the PlayerController and PlayerMotor Scripts, there error in the scripts.

  • edited September 2020

    I cant really tell without seeing the script but it looks like you put [SerializedFieldAttribute] instead of [SerializedField], or you serialized something that cant be (static fields or properties). You also cant searialize something outside of this list:

    - All basic data types, such as int, string, float, bool.

    - Some built-in types, such as Vector2, Vector3, Vector4, Quaternion, Matrix4x4, Color, Rect, LayerMask.

    - Arrays of a serializable type

    - Lists of a serializable type

    - Enums

    - Structs

    for more info on serialization you can got to the unity documentation on that subject: https://docs.unity3d.com/ScriptReference/SerializeField.html

  • Thanks to all. I still cant fix it. Here are the scripts. Thnaks again for the help


  • you have [SerializedField]/[SerializeaField] it should be [SerializeField] with no d. You also typed require component wrong in the player motor. I think you are mainly having problems with little typos you should look into how to enable auto complete and debugging in whatever IDE you use.

  • Thanks again. I cant believe it. I checked multiple times and didnt notice.

    But Now Unity points me new errors:

    I want to keep going but Im stucked. Sorry for my rookieness!!

  • A thousand thanks!! I will investigate as you recommend

Sign In or Register to comment.