Search Unity

Help me .....I have a problem with my script

Discussion in 'Scripting' started by GamersConnect, Jun 18, 2020.

  1. GamersConnect

    GamersConnect

    Joined:
    Jun 2, 2020
    Posts:
    11
    Error :NullReferenceException: Object reference not set to an instance of an object Player.Update () (at Assets/Player.cs:43)

    Script :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [RequireComponent(typeof(MoveController))]
    6. public class Player : MonoBehaviour
    7. {
    8.  
    9.     [System.Serializable]
    10.     public class MouseInput
    11.     {
    12.     public Vector2 Damping;
    13.     public Vector2 Sensitivity;
    14.     }
    15.  
    16.     [SerializeField]float speed;
    17.     [SerializeField]MouseInput MouseControl;
    18.  
    19.     private MoveController m_MoveController;
    20.     public MoveController MoveController {
    21.     get
    22.     {
    23.         if(m_MoveController == null)
    24.         m_MoveController = GetComponent<MoveController>();
    25.         return m_MoveController;
    26.     }
    27.     }
    28.  
    29.     InputController playerInput;
    30.  
    31.     void Start()
    32.     {
    33.     GameManager.Instance.LocalPlayer = this;
    34.     }
    35.  
    36.     void Awake()
    37.     {
    38.     playerInput = GameManager.Instance.InputController;
    39.     }
    40.  
    41.     void Update()
    42.     {
    43.     Vector2 direction = new Vector2(playerInput.Vertical * speed, playerInput.Horizontal * speed);
    44.     MoveController.Move(direction);
    45.     }
    46. }
    47.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    GamersConnect likes this.
  3. GamersConnect

    GamersConnect

    Joined:
    Jun 2, 2020
    Posts:
    11
    Thanks a lot ....I will read it ;)
     
  4. GamersConnect

    GamersConnect

    Joined:
    Jun 2, 2020
    Posts:
    11
  5. Zer0Cool

    Zer0Cool

    Joined:
    Oct 24, 2014
    Posts:
    203
    Code (CSharp):
    1.     void Awake()
    2.     {
    3.       playerInput = GameManager.Instance.InputController; // Here you try to get an instance from the GameManager class and i assume this isnt working
    4.     }
    I assume you dont have a GameManager GO in your scene with the GameManager class (script) attached. Mostly these OameObject are wrong handled and then they are not in the current scene and therefore you cant get an instance from this class in the above code and therefore playerInput instance is NULL too.
     
    GamersConnect likes this.
  6. GamersConnect

    GamersConnect

    Joined:
    Jun 2, 2020
    Posts:
    11
    Wait ......This is the Game manager Script....
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. public class GameManager {
    5.  
    6.     public event System.Action<Player> OnLocalPlayerJoined;  
    7.     private GameObject gameObject;
    8.  
    9.     private static GameManager m_Instance;
    10.     public static GameManager Instance {
    11.     get {
    12.         if (m_Instance == null)
    13.         {
    14.         m_Instance = new GameManager();
    15.         m_Instance.gameObject = new GameObject("_gameManager");
    16.         m_Instance.gameObject.AddComponent<InputController>();
    17.         }
    18.         return m_Instance;
    19.     }
    20.     }
    21.     private InputController m_InputController;
    22.     public InputController InputController
    23.     {
    24.     get
    25.     {
    26.         if(m_InputController = null)
    27.         m_InputController = gameObject.GetComponent<InputController>();
    28.         return m_InputController;  
    29.     }
    30.     }
    31.  
    32.     private Player m_LocalPlayer;
    33.     public Player LocalPlayer
    34.     {
    35.     get
    36.     {
    37.         return m_LocalPlayer;
    38.     }
    39.     set
    40.     {
    41.         m_LocalPlayer = value;
    42.         if(OnLocalPlayerJoined != null)
    43.         OnLocalPlayerJoined(m_LocalPlayer);
    44.     }
    45.     }
    46. }
    47.  
    Thanks if you check that :)
     
  7. GamersConnect

    GamersConnect

    Joined:
    Jun 2, 2020
    Posts:
    11
    Ana wait ..... I will give you all of the script to see .....
    Code (CSharp):
    1. //GameManager
    2.  
    3. using System.Collections;
    4. using UnityEngine;
    5.  
    6. public class GameManager {
    7.  
    8.     public event System.Action<Player> OnLocalPlayerJoined;  
    9.     private GameObject gameObject;
    10.  
    11.     private static GameManager m_Instance;
    12.     public static GameManager Instance {
    13.     get {
    14.         if (m_Instance == null)
    15.         {
    16.         m_Instance = new GameManager();
    17.         m_Instance.gameObject = new GameObject("_gameManager");
    18.         m_Instance.gameObject.AddComponent<InputController>();
    19.         }
    20.         return m_Instance;
    21.     }
    22.     }
    23.     private InputController m_InputController;
    24.     public InputController InputController
    25.     {
    26.     get
    27.     {
    28.         if(m_InputController = null)
    29.         m_InputController = gameObject.GetComponent<InputController>();
    30.         return m_InputController;  
    31.     }
    32.     }
    33.  
    34.     private Player m_LocalPlayer;
    35.     public Player LocalPlayer
    36.     {
    37.     get
    38.     {
    39.         return m_LocalPlayer;
    40.     }
    41.     set
    42.     {
    43.         m_LocalPlayer = value;
    44.         if(OnLocalPlayerJoined != null)
    45.         OnLocalPlayerJoined(m_LocalPlayer);
    46.     }
    47.     }
    48. }
    49.  
    Code (CSharp):
    1. //InputController
    2.  
    3. using System.Collections;
    4. using UnityEngine;
    5.  
    6. public class InputController : MonoBehaviour
    7. {
    8.    
    9.     public float Vertical;
    10.     public float Horizontal;
    11.     public Vector2 MouseInput;
    12.  
    13.     void Update()
    14.     {
    15.      Vertical = Input.GetAxis("Vertical");
    16.      Horizontal = Input.GetAxis("Horizontal");
    17.      MouseInput = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
    18.     }
    19. }
    20.  
    Code (CSharp):
    1. //Player
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. [RequireComponent(typeof(MoveController))]
    8. public class Player : MonoBehaviour
    9. {
    10.  
    11.     [System.Serializable]
    12.     public class MouseInput
    13.     {
    14.     public Vector2 Damping;
    15.     public Vector2 Sensitivity;
    16.     }
    17.  
    18.     [SerializeField]float speed;
    19.     [SerializeField]MouseInput MouseControl;
    20.  
    21.     private MoveController m_MoveController;
    22.     public MoveController MoveController {
    23.     get
    24.     {
    25.         if(m_MoveController == null)
    26.         m_MoveController = GetComponent<MoveController>();
    27.         return m_MoveController;
    28.     }
    29.     }
    30.  
    31.     InputController playerInput;
    32.  
    33.     void Start()
    34.     {
    35.     GameManager.Instance.LocalPlayer = this;
    36.     }
    37.  
    38.     void Awake()
    39.     {
    40.     playerInput = GameManager.Instance.InputController;
    41.     }
    42.  
    43.     void Update()
    44.     {
    45.     Vector2 direction = new Vector2(playerInput.Vertical * speed, playerInput.Horizontal * speed);
    46.     MoveController.Move(direction);
    47.     }
    48. }
    49.  
    Code (CSharp):
    1. //MoveController
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class MoveController : MonoBehaviour
    8. {
    9.    
    10.     public void Move(Vector2 direction)
    11.     {
    12.     transform.position += transform.forward * direction.x * Time.deltaTime +
    13.          transform.right * direction.y * Time.deltaTime;  
    14.     }
    15. }
    16.  
    If you really check all these ...You will help me A LOT !!!!!!!!!!!!
     
  8. Zer0Cool

    Zer0Cool

    Joined:
    Oct 24, 2014
    Posts:
    203
    Change this in your Player class:

    Code (CSharp):
    1.     void Start()
    2.     {
    3.       GameManager.Instance.LocalPlayer = this;
    4.       playerInput = GameManager.Instance.InputController;
    5.     }
    6.  
     
    Last edited: Jun 19, 2020
  9. GamersConnect

    GamersConnect

    Joined:
    Jun 2, 2020
    Posts:
    11
    Thanks I found another way to make the character controller

    Sorry for the late reply