I've created two scripts. One includes a variable and a method. Second script's task is to call the first script and access its component. However I'm getting the following error :
ThisScriptWillCallAnotherScript.Update () (at Assets/Scripts/ThisScriptWillCallAnotherScript.cs:21)
I tried removing the line it's referring to but the error persists. Any idea what I may be doing wrong?
Script 1 :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThisScriptWillBeCalledInAnotherScript : MonoBehaviour {
public string accessMe = "this variable has been accessed from another script";
public void AccessThisMethod () {
Debug.Log ("This method has been accessed from another script.");
}
}
Script 2 :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThisScriptWillCallAnotherScript : MonoBehaviour {
// below we are calling a script and giving a name//
ThisScriptWillBeCalledInAnotherScript callingAScript;
void Start () {
//here we are using GetComponent to access the script//
callingAScript = GetComponent<ThisScriptWillBeCalledInAnotherScript> ();
Debug.Log ("Please press enter key...");
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.Return)) {
Debug.Log ("this is the script we just called " + callingAScript);
Debug.Log (callingAScript.accessMe); // we are accessing a variable of the script we called
callingAScript.AccessThisMethod (); // we are calling a method of the script we called
}
}
}