Search Unity

Question Object reference not set to an instance of an object

Discussion in 'Scripting' started by ashutosh27482, Jun 6, 2020.

  1. ashutosh27482

    ashutosh27482

    Joined:
    Jun 6, 2020
    Posts:
    1
    Hello Guys,
    I am trying to access variable : missionName from my AKMissionData script to AKMissionSystem script.
    But getting error : Object reference not set to an instance of an object.
    Please Help!!!


    Code (CSharp):
    1. public class AKMissionData : MonoBehaviour
    2. {
    3.     //Mission Info
    4.     public int missionOrder;
    5.     public string missionName;
    6.     public string missionDescription;
    7.  
    8.     public GameObject missionGiver;
    9.  
    10.     public int missionTimer;
    11.     public int missionReward;
    12.  
    13.     public enum types { Reach, Kill, Gather, Save, Defend }
    14.  
    15.     public types missionType;
    16.     public GameObject missiontarget;
    17.     public int  missionTargetAmount;
    18.     public bool missionStartWithGame;
    19.     // Start is called before the first frame update
    20.     void Start()
    21.     {
    22.         missionName = "mission Name Here";
    23.     }
    24.  
    25.     // Update is called once per frame
    26.     void Update()
    27.     {
    28.      
    29.     }
    30. }
    31.  
    Trying to get variable from above script to below script :


    Code (CSharp):
    1. public class AKMissionSystem : MonoBehaviour
    2. {
    3.     [Header("Mission UI")]
    4.     public Text missionInfoTxt;
    5.     private string missionName;
    6.  
    7.  
    8.  
    9.     void Start()
    10.     {
    11.         GameObject go = GameObject.FindGameObjectWithTag("MissionData");
    12.         missionName = go.GetComponent<AKMissionData>().missionName;
    13.      
    14.         Debug.Log(missionName);
    15.         missionInfoTxt.text = missionName;
    16.  
    17.     }
    18.  
    19.  
    20.  
    21. }
    Error :
    NullReferenceException: Object reference not set to an instance of an object
    AKMissionSystem.Start () (at Assets/AKMissionSystem.cs:17)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745