Search Unity

How to use a function from other script?

Discussion in 'Getting Started' started by unity_ITKAeZaFsbPmSQ, Aug 27, 2020.

  1. unity_ITKAeZaFsbPmSQ

    unity_ITKAeZaFsbPmSQ

    Joined:
    Aug 27, 2020
    Posts:
    3
    I have one script that uses a lot of objects and script with the raycast in it. How can I use a function from first script in the second that applies to RaycastHit object? (RaycastHit object have first script)
    Just like that:
    Code (CSharp):
    1.  Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    2. rayOut = Physics.Raycast(ray, out hitted, rayDistance);
    3. if (rayOut && hitted.collider.tag == "Snaptable")
    4.         {
    5.             //something that uses a function
    6.         }
    7.  
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Where your "//something" comment is, do something like:

    Code (csharp):
    1. var snap = hitted.collider.GetComponentInParent<Snaptable>();
    2. snap.DoSomething();
     
  3. unity_ITKAeZaFsbPmSQ

    unity_ITKAeZaFsbPmSQ

    Joined:
    Aug 27, 2020
    Posts:
    3