Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

m_Instance.GetComponent<object>() returns null or nothing?

Discussion in 'Getting Started' started by larjun, Mar 26, 2020.

  1. larjun

    larjun

    Joined:
    Feb 20, 2017
    Posts:
    3
    Hi!
    I am trying to reorganizing and customize my code to use Unitys teaching project 'Tank's overall structure.
    GameManager (Main), LevelManager with CoRoutines, EnemyManager, Enemy Movement and Player
    (standalone) and some more classes ...
    Exchange info between EnemyMovement and Player class.
    Game player control the Player class.
    Enemies[x] (with EnemyMovement) should trigger on Players position if everything is as planned.

    But when I applied my old Enemy class to EnemyManager into EnemyMovement then
    I get 'Object reference not set to an instance of an object'.

    Could anyone give me some hint?
    Thanks in advance!

    (This code works in Tanks so something is not ok in my EnemyMovement but what (it's to large to show all code). But it have Start, Update, FixedUpdate and OnTrigger and a lots och metods.)

    Code (csharp):
    1.  
    2. namespace GAME11
    3. {
    4.     public class LevelManager : MonoBehaviour
    5.     {
    6.         public EnemyManager[] m_Enemies;        // A collection of enemies for control these objects.
    7.         public GameObject m_EnemyPrefab;        // Reference to prefab.
    8.         private void SpawnAllEnemies()
    9.         {
    10.             for (int i = 0; i < m_Enemies.Length; i++)
    11.             {
    12.                 m_Enemies[i].m_Instance = Instantiate(m_EnemyPrefab, m_Enemies[i].m_SpawnPoint.position, m_Enemies[i].m_SpawnPoint.rotation) as GameObject;
    13.                 m_Enemies[i].m_EnemyNumber = i + 1;
    14.                 m_Enemies[i].Setup();
    15.        ...
    16.             }
    17.         }
    18.         ...
    19.     }
    20. }        
    21.  
    22. //In EnemyManager
    23.  
    24. namespace GAME11
    25. {
    26.     [Serializable]
    27.     public class EnemyManager
    28.     {
    29.         private EnemyMovement m_Movement;  
    30.         [HideInInspector] public int m_EnemyNumber; // This specifies which enemy this manager
    31.                            // is for in enemynumber.
    32.  
    33.         public void Setup()
    34.         {
    35.             m_Movement = m_Instance.GetComponent<EnemyMovement>();  //problems
    36.             try
    37.             {
    38.                 m_Movement.m_EnemyNumber = m_EnemyNumber;  //error appear (m_Movement == null)
    39.             }
    40.             catch (Exception e)
    41.             {
    42.                 HandleTextFile.WriteString("e=" + e.Message);   //message='Object reference not set to an instance of an object'
    43.             }
    44.         }
    45.     }
    46. }
    47.  
    48. namespace GAME11
    49. {
    50.     public class EnemyMovement : MonoBehaviour
    51.     {
    52.         public int m_EnemyNumber;   // Used to identify Enemy. This is set by this Enemy's manager.
    53.         //code for moving Enemies , communicate via metods in Player and gives Player info about Enemy
    54.     }
    55. }
    56. [end code]
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,851
    Yes, Unity can! When you get that, double-click on the error message. It will take you right to the line with the problem. That line will probably have only one or a very small number of object references on it; just see which one is nil (by adding some Debug.Logs right before that line, if necessary).
     
  3. larjun

    larjun

    Joined:
    Feb 20, 2017
    Posts:
    3
    Thanks for your answer!

    In catch (Exception e)

    "System.NullReferenceException: Object reference not set to an instance of an object\r\n at GAME11.EnemyManager.Setup () [0x00057] in
    C:\\Users\\Lars XXXXXX\\Desktop\\YYYYY game\\Assets\\Scripts\\EnemyManager.cs:38 "

    When breakpointing row 38 m_Movement is null it never get any value from m_Instance.GetComponent<EnemyMovement>.
    Code (csharp):
    1.  
    2. m_Movement = m_Instance.GetComponent<EnemyMovement>();  //problems
    3. [end code]
    4.  
    5. Only Player class is active. Could verify that in my tracefile. No activity in EnemyMovement.
    6.  
    7. Why return GetComponent null or maybe it can't because EnemyMovement have some problem that compiler not could tell.
    8. Must work harder with this issue!