Home>

Since there were multiple scenes, I wanted to standardize the processing of the object clicked by Ray
A script for ray processing
Execution processing of objects that hit B script
I tried to create it separately, but the B script cannot identify the object.

Because I understand to some extent that update itself is not a constant call
I understand that the timing is not correct and I have not been able to obtain it.
To reliably monitor objects hit by A script from B script
What ideas do you have?

AScript.cs

public GameObject HitGameObject;
void Update () {
 var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 RaycastHit HitObject;
 if (Physics.Raycast (ray, out HitObject))
 {
   Debug.LogFormat ("hit with {0}", HitObject);
   HitGameObject = HitObject.collider.gameObject;
   HitGameObject = null;
 }
}

BScript.cs

void Update () {
 if (A.HitGameObject! = null)
 {
    Debug.Log (A.HitGameObject.name);
    audioSource.PlayOneShot (audioClip);
 }
}
  • Answer # 1

    When "Check script B object for each frame in script B", execution order control between scripts becomes a problem.

    If you just want to solve it, you can change the script BUpdatetoLateUpdatein order to "execute script B processing after script A". There is.


    A more advanced solution would be to useC # Eventto notify that HitGameObject has changed. I think that you will acquire better implementation skills that match the learning costs, so please challenge yourself.

    What are delegates and events [C #]
    http://kan-kikuchi.hatenablog.com/entry/Delegate

    (The explanation of Event is scrolled more than half of the page. You can skip to that point, but since Delegate is the basis of Event, you can read from above if possible. I think.)

    The advantage of using the Event mechanism is that you can separate "the side that knows when to process" and "the side that knows what you want to process" I think you can mention.

    If you think about how to use Event by revising the question, for example,

    Script A issues the event "Ray hits an object"

    Enable script B to receive script A events

    I think it will be an implementation like

    .

    Here is the code for the reference example.

    // Script A (Expose the mouse and the covered object as an event)
    public delegate void OnMouseHitEventHanlder (GameObject hitObject);
    public event OnMouseHitEventHanlder OnMouseHit;
    public GameObject HitGameObject;
    void Update ()
    {
        var hitObject = GetOnMouseHitGameObject ();
        HandleOnMouseHit (hitObject);
        HitGameObject = hitObject;
    }
    void HandleOnMouseHit (GameObject hitObject)
    {
        if (hitObject! = null)
        {
            Debug.LogFormat ("hit with {0}", hitObject);
            // issue event
            OnMouseHit? .Invoke (hitObject);
        }
    }
    GameObject GetOnMouseHitGameObject ()
    {
        var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        return Physics.Raycast (ray, out var hitObject)
            ? hitObject.collider.gameObject
            : null
            ;
    }
    // Script B (executes processing in response to Script A event)
    void Start ()
    {
        A.OnMouseHit + = OnMouseHitAction;
    }
    void OnDestroy ()
    {
        A.OnMouseHit-= OnMouseHitAction;
    }
    void OnMouseHitAction (GameObject hitObject)
    {
        Debug.Log (hitObject.name);
        audioSource.PlayOneShot (audioClip);
    }