Home>

I want to call the A () function defined in the GameManager script with a TimeScript script.

Thus, an error occurs when playing
Error details are
NullReferenceException: Object reference not set to an instance of an object
TimeScript.Update () (at Assets/Scripts/TimeScript.cs: 37)
There is.
The following is the script to access.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TimeScript: MonoBehaviour
{
    // public GameObject gameobject;
    GameObject gameobject;
    GameObject limitTimeUI;
    float time = 5.0f;
    // Start is called before the first frame update
    void Start ()
    {
        limitTimeUI = GameObject.Find ("LimitTimeUI");
        gameobject = GameObject.Find ("Time");
        // bool B = gameObject.GetComponent<GameManager>(). isBallMoving;
        //Debug.Log(B);
        // B = false;
        //gameobject.GetComponent<GameManager>(). ReturnAccess ();
    }
    // Update is called once per frame
    void Update ()
    {
        // Decrease remaining time every frame
            this.time-= Time.deltaTime;
            if (this.time<0) {
            this.limitTimeUI.GetComponent<Text>(). text = "Time: 0";
            GameManager a = GetComponent<GameManager>();
            a.A ();
            }
            else
            {
                // Display the text converted from time to string
                this.limitTimeUI.GetComponent<Text>(). text = "Time:" + this.time.ToString ("F0");
            }
        }

}


This is the script to be accessed.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameManager: MonoBehaviour {
    public int StageNo;
    public bool isBallMoving;
    / * public void ReturnAccess ()
    {
        Debug.Log ("Access successful!");
    } * /
    public GameObject ballPrefab;
    public GameObject ball;
    public GameObject goButton;
    public GameObject retryButton;
    public GameObject clearText;
    public Collider2D movable1;
    public Collider2D movable2;
    public AudioClip clearSE;
    public AudioClip unmoveSE;
    public AudioClip moveSE;
    private AudioSource audioSource;
    // private Text timeText;
    // private float time = 30;
    // additional code from here
    public HandGestureManager _handGestureManager;
    public HandControl _hand;
    // Use this for initialization
    void Start () {
        retryButton.SetActive (false);
        isBallMoving = false;
        audioSource = gameObject.GetComponent  ();
        // movable1 = GetComponent<Collider2D>();
        // movable2 = GetComponent<Collider2D>();
        //GetComponent<Text>().text = ((int) time) .ToString ();
    }
    // Update is called once per frame
    void Update () {
    }
    public void PushGoButton () {
        // if (Input.GetKeyDown (KeyCode.Space)) {
            //Debug.Log("space ");
       //}
            Rigidbody2D rd = ball.GetComponent<Rigidbody2D>();
            rd.isKinematic = false;
            retryButton.SetActive (true);
            goButton.SetActive (false);isBallMoving = true;
            movable1.isTrigger = false;
            movable2.isTrigger = false;

    }
    public void PushRetryButton () {
        Destroy (ball);
        ball = (GameObject) Instantiate (ballPrefab);
        retryButton.SetActive (false);
        goButton.SetActive (true);
        isBallMoving = false;
    }
    public void PushBackButton () {
        GobackStageSelect ();
    }

    public void StageClear () {
        audioSource.PlayOneShot (clearSE);
        if (PlayerPrefs.GetInt ("CLEAR", 0)<StageNo) {
            PlayerPrefs.SetInt ("CLEAR", StageNo);
        }
        clearText.SetActive (true);
        retryButton.SetActive (false);
        Invoke ("GobackStageSelect", 3.0f);
    }
    void GobackStageSelect () {
        SceneManager.LoadScene ("StageSelectScene");
    }
    public void TouchUnMoveBox () {
        audioSource.PlayOneShot (unmoveSE);
    }
    public void TouchMoveBox () {
        audioSource.PlayOneShot (moveSE);
    }
    public void A ()
    {
        Rigidbody2D rd = ball.GetComponent<Rigidbody2D>();
        rd.isKinematic = false;
        //retryButton.SetActive(true);
        //goButton.SetActive(false);
        isBallMoving = true;
        movable1.isTrigger = false;
        movable2.isTrigger = false;
    }
}

I will add any necessary information, so please give me some advice.

  • Answer # 1

    Timescript variable definition
    Declare GameManager g;

    In the Start function
    g = GameObjct.Find ("GameManager"). Getcomponent();
    will do.

    Then
    g.A ();
    You can call it.

Related articles