Search Unity

Execute a event in a if statement

Discussion in 'Scripting' started by V-J, Oct 1, 2019.

  1. V-J

    V-J

    Joined:
    Apr 1, 2015
    Posts:
    73
    How can i execute / call a event from another script in a if statement?


    Code (CSharp):
    1.  // Update is called once per frame
    2.     void Update()
    3.     {
    4.         if (OVRInput.Get(OVRInput.Button.SecondaryIndexTrigger))
    5.         {          
    6.             SetEventMaskToShow();
    7.             laserPointer.SetActive(true);
    8.  
    9.             if (OVRInput.GetDown(OVRInput.Button.One))
    10.             {
    11.                 //call event
    12.  
    13.             }
    14.            
    15.  
    16.         }

    Code (CSharp):
    1.     public void OnPointerDown(PointerEventData eventData)
    2.    
    3.     {
    4.      
    5.  
    6.        selectedObject = eventData.pointerCurrentRaycast.gameObject;
    7.  
    8.     }
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Hmmm. I recommend you revisit some of the lierature in order to ensure we are all using the same terminology. I'm assuming here that you are not really interesting in 'calling an event', but rather call (or 'invoke') a method that is in another script (component).

    The key here is to remember the name of the class you gave that script - that's what you write before ': Monobehaviour' at the very beginning of your class. Let's say you named that class myCoolScript. In that class you define a method 'CallMeFromSomplaceElse()'.

    All you need to do now is to get a 'reference' to the script. Since it's a class, you can simply defne in in your script. If you define it as a public variable, you can drag the script into the Slot with Editor. Or, during runtime, you can gain access to the script with GetComponent<myCoolScript>.

    Once you have a reference to your external script, you can invoke the method using the Standard dot notation:

    Code (CSharp):
    1. myCoolScript scriptRef = gameObject.GetComponent<myCoolScript>(); // get Access to script in this GameObject
    2.  
    3. scriptRef.CallMeFromSomeplaceElse(); // invoke that method on that script
    4.  
    There is no difference when invoking it in an if statement.

    If you need it as part of an expression during evaluation in an if Statement, make sure that the method you want to invoke returns true or false (a bool)

    Invoking functions that are interpreted as events is no other than invoking any other methods, just make sure you pass the correct Parameters. So in your case you need the reference to the script that has the OnPointerDown method defined, and simply invoke it via

    Code (CSharp):
    1. theReferencedEventScript.OnPointerDown(...)
     
    Last edited: Oct 1, 2019
  3. V-J

    V-J

    Joined:
    Apr 1, 2015
    Posts:
    73
    What kind of parameters can i pass in?

    https://docs.unity3d.com/2018.2/Documentation/ScriptReference/EventSystems.PointerEventData.html
    I'm not a programmer, but i understand some code.
     
  4. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
  5. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (OVRInput.Get(OVRInput.Button.SecondaryIndexTrigger))
    4.         {        
    5.             SetEventMaskToShow();
    6.             laserPointer.SetActive(true);
    7.             if (OVRInput.GetDown(OVRInput.Button.One))
    8.             {
    9.                  theReferencedEventScript.logicMethodName(gameObject);
    10.             }
    11.          
    12.         }
    Code (CSharp):
    1.     public void OnPointerDown(PointerEventData eventData)
    2.  
    3.     {
    4.    
    5.        selectedObject = eventData.pointerCurrentRaycast.gameObject;
    6.        logicMethodName(selectedObject);
    7.     }
    8.  
    9.    public void logicMethodName(GameObject target) {
    10.        // do the job here
    11.        }
     
  6. V-J

    V-J

    Joined:
    Apr 1, 2015
    Posts:
    73
    @palex-nx Thank you so much for your help! it works.. :)
     
  7. V-J

    V-J

    Joined:
    Apr 1, 2015
    Posts:
    73
    @palex-nx i get the following error in the console.

    NullReferenceException: Object reference not set to an instance of an object
    InputLaserPointer.Update () (at Assets/Scripts/InputLaserPointer.cs:36)

    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (OVRInput.Get(OVRInput.Button.SecondaryIndexTrigger))
    4.         {          
    5.             SetEventMaskToShow();
    6.             laserPointer.SetActive(true);
    7.  
    8.             if (OVRInput.GetDown(OVRInput.Button.One))
    9.             {
    10.                 dataTransfer.GetSelectedGO(gameObject); // 36
    11.  
    12.             }
    13.            
    14.  
    15.         }
    16.         else
    17.  
    18.         {
    19.             //SetEventMaskToNothing();
    20.             laserPointer.SetActive(false);
    21.  
    22.         }
    23.     }
     
  8. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Where is line 36? Probably 20 in your code snipped.

    The error means that lasterPointer (line 20) is not initialized (is "empty"). Did you get and verify it correctly, or simply forgot to connect via Editor (if it's a public variable)?
     
    Last edited: Oct 2, 2019
  9. V-J

    V-J

    Joined:
    Apr 1, 2015
    Posts:
    73
    @csofranz All variables are in place, and i copied the whole script what makes it more clear.
    It is line 36 what give the error.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5.  
    6.  
    7. public class InputLaserPointer : MonoBehaviour
    8. {
    9.     public GameObject laserPointer;
    10.  
    11.     DataTransfer dataTransfer;
    12.  
    13.     public OVRPhysicsRaycaster _ovrPhysicsRaycaster;
    14.  
    15.     //public GameObject selectedObject;
    16.  
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.  
    21.         int oldeventmask = _ovrPhysicsRaycaster.eventMask;
    22.  
    23.     }
    24.  
    25.  
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.         if (OVRInput.Get(OVRInput.Button.SecondaryIndexTrigger))
    30.         {        
    31.             SetEventMaskToShow();
    32.             laserPointer.SetActive(true);
    33.  
    34.             if (OVRInput.GetDown(OVRInput.Button.One))
    35.             {
    36.                 dataTransfer.GetSelectedGO(gameObject);
    37.  
    38.             }
    39.          
    40.  
    41.         }
    42.         else
    43.  
    44.         {
    45.             //SetEventMaskToNothing();
    46.             laserPointer.SetActive(false);
    47.  
    48.         }
    49.     }
    50.  
    51.     void SetEventMaskToShow()
    52.     {
    53.         _ovrPhysicsRaycaster.eventMask |= (9 << 8);
    54.     }
    55.  
    56.     void SetEventMaskToNothing()
    57.     {
    58.         _ovrPhysicsRaycaster.eventMask |= (7 << 0);
    59.     }
    60.  
    61.  
    62. }
    Code (CSharp):
    1.  public void OnPointerDown(PointerEventData eventData)
    2.    
    3.     {
    4.  
    5.         selectedObject = eventData.pointerCurrentRaycast.gameObject;
    6.         //propData.text = selectedObject.transform.root.GetComponentInChildren<BimMetadata>().property.ToString();
    7.         //valData.text = selectedObject.transform.root.GetComponentInChildren<BimMetadata>().value.ToString();
    8.  
    9.         GetSelectedGO(selectedObject);
    10.  
    11.     }
    12.  
    13.     public void GetSelectedGO(GameObject target)
    14.     {
    15.         HighLiteGo();
    16.  
    17.         propData.text = target.transform.root.GetComponentInChildren<BimMetadata>().property.ToString();
    18.         valData.text = target.transform.root.GetComponentInChildren<BimMetadata>().value.ToString();
     
  10. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    It looks as if you are not initializing the variable 'dataTransfer' (line 36). I see where it is defined (line 11). Where do you set it up with a value?
     
  11. V-J

    V-J

    Joined:
    Apr 1, 2015
    Posts:
    73
    @csofranz DataTransfer is the name of the class. I created a variable of it to acces the GetSelectedObject()

    Code (CSharp):
    1. public class DataTransfer : MonoBehaviour, IPointerDownHandler
    2. {
    3.  
    4.     //public Text goPropData;
    5.     //public Text goValData;
    6.  
    7.     private Text propData;
    8.     private Text valData;
    9.  
    10.     public GameObject selectedObject;
    11.     public Material selectMat;
    12.     private Material originalMat;
    13.  
    14.    
     
    Last edited: Oct 2, 2019
  12. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Yes, but where are you allocating it, e.g.

    Code (CSharp):
    1. dataTransfer = new DataTransfer();
    ?
     
  13. V-J

    V-J

    Joined:
    Apr 1, 2015
    Posts:
    73
    @csofranz Ah ok, i didnt, where do i need to place this line of code? is it in the start function of the InputLaserPointer.cs?
     
    Last edited: Oct 2, 2019
  14. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    It seems to be a global, so initializing it in Start() would be a good idea.