0

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
        }
    }
}
Alex Myers
  • 6,196
  • 7
  • 23
  • 39
Mh.A
  • 11
  • 2
  • The error you posted is no error. it's just a stack trace - We need the actual error message – Patrick Hollweck Feb 20 '19 at 07:01
  • @PatrickHollweck that's all im getting. NullReferenceException: Object reference not set to an instance of an object – Mh.A Feb 20 '19 at 07:14
  • You will have to reference a GameObject that actually has the Component `ThisScriptWillBeCalledInAnotherScript` – Luchspeter Feb 20 '19 at 07:19
  • @Eric.Volli I dont quite follow. Does that mean that `thatThisScriptWillBeCalledInAnotherScript` should be attached to a game object and then make a reference to that game object? – Mh.A Feb 20 '19 at 07:24
  • I mean it depends on what you want to achieve. If the `ThisScriptWillBeCalledInAnotherScript` accesses GameObject properties like `Transform` or so, it has to be attached to a GameObject. If it is a HelperClass only, then you can simply create an Instance from it with the `new` Keyword (But then the Script should not inherit from `Monobehaviour`) – Luchspeter Feb 20 '19 at 07:31
  • Usually if `GetComponent` returns null, it means that the current GameObject does not have this Component. – Luchspeter Feb 20 '19 at 07:32

1 Answers1

1

It Unity GameObjects can have Components. The method GetComponent<T>() gets a reference to the component T from the current GameObject.

So if your GameObject has both components (ScriptAand ScriptB)

enter image description here

then this will return a "not-null" reference to the instance of ScriptB:

public class ScriptA : MonoBehaviour {

    ScriptB scriptB;

    // Use this for initialization
    void Start () {
        scriptB = GetComponent<ScriptB>(); //Not null if GameObject has ScriptB component.
    }
}

If you GameObject does not have the component ScriptB, then the Method GetComponent<T>() will return null.

If ScriptB is a component from another GameObject then you will need a reference to that other GameObject and call it via OtherGamoeObject.GetComponent<T>() If ScriptB is not even a Script that changed the GameObject and simply (for example) contains some Math-Calculations or so, then I would suggest not making it inherit from Monobehaviourand simply creating an instance like so: var scriptB = new ScriptB();

Luchspeter
  • 744
  • 7
  • 24