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
        }
    }
}
6
  • The error you posted is no error. it's just a stack trace - We need the actual error message Commented Feb 20, 2019 at 7:01
  • @PatrickHollweck that's all im getting. NullReferenceException: Object reference not set to an instance of an object Commented Feb 20, 2019 at 7:14
  • You will have to reference a GameObject that actually has the Component ThisScriptWillBeCalledInAnotherScript Commented Feb 20, 2019 at 7: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? Commented Feb 20, 2019 at 7: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) Commented Feb 20, 2019 at 7:31

1 Answer 1

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();

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.