How can i execute / call a event from another script in a if statement? Code (CSharp): // Update is called once per frame void Update() { if (OVRInput.Get(OVRInput.Button.SecondaryIndexTrigger)) { SetEventMaskToShow(); laserPointer.SetActive(true); if (OVRInput.GetDown(OVRInput.Button.One)) { //call event } } Code (CSharp): public void OnPointerDown(PointerEventData eventData) { selectedObject = eventData.pointerCurrentRaycast.gameObject; }
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): myCoolScript scriptRef = gameObject.GetComponent<myCoolScript>(); // get Access to script in this GameObject scriptRef.CallMeFromSomeplaceElse(); // invoke that method on that script 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): theReferencedEventScript.OnPointerDown(...)
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.
Well, according to your own example code: Code (CSharp): public void OnPointerDown(PointerEventData eventData) { } it would be a variable of type PointerEventData
Code (CSharp): void Update() { if (OVRInput.Get(OVRInput.Button.SecondaryIndexTrigger)) { SetEventMaskToShow(); laserPointer.SetActive(true); if (OVRInput.GetDown(OVRInput.Button.One)) { theReferencedEventScript.logicMethodName(gameObject); } } Code (CSharp): public void OnPointerDown(PointerEventData eventData) { selectedObject = eventData.pointerCurrentRaycast.gameObject; logicMethodName(selectedObject); } public void logicMethodName(GameObject target) { // do the job here }
@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): void Update() { if (OVRInput.Get(OVRInput.Button.SecondaryIndexTrigger)) { SetEventMaskToShow(); laserPointer.SetActive(true); if (OVRInput.GetDown(OVRInput.Button.One)) { dataTransfer.GetSelectedGO(gameObject); // 36 } } else { //SetEventMaskToNothing(); laserPointer.SetActive(false); } }
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)?
@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): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class InputLaserPointer : MonoBehaviour { public GameObject laserPointer; DataTransfer dataTransfer; public OVRPhysicsRaycaster _ovrPhysicsRaycaster; //public GameObject selectedObject; // Start is called before the first frame update void Start() { int oldeventmask = _ovrPhysicsRaycaster.eventMask; } // Update is called once per frame void Update() { if (OVRInput.Get(OVRInput.Button.SecondaryIndexTrigger)) { SetEventMaskToShow(); laserPointer.SetActive(true); if (OVRInput.GetDown(OVRInput.Button.One)) { dataTransfer.GetSelectedGO(gameObject); } } else { //SetEventMaskToNothing(); laserPointer.SetActive(false); } } void SetEventMaskToShow() { _ovrPhysicsRaycaster.eventMask |= (9 << 8); } void SetEventMaskToNothing() { _ovrPhysicsRaycaster.eventMask |= (7 << 0); } } Code (CSharp): public void OnPointerDown(PointerEventData eventData) { selectedObject = eventData.pointerCurrentRaycast.gameObject; //propData.text = selectedObject.transform.root.GetComponentInChildren<BimMetadata>().property.ToString(); //valData.text = selectedObject.transform.root.GetComponentInChildren<BimMetadata>().value.ToString(); GetSelectedGO(selectedObject); } public void GetSelectedGO(GameObject target) { HighLiteGo(); propData.text = target.transform.root.GetComponentInChildren<BimMetadata>().property.ToString(); valData.text = target.transform.root.GetComponentInChildren<BimMetadata>().value.ToString();
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?
@csofranz DataTransfer is the name of the class. I created a variable of it to acces the GetSelectedObject() Code (CSharp): public class DataTransfer : MonoBehaviour, IPointerDownHandler { //public Text goPropData; //public Text goValData; private Text propData; private Text valData; public GameObject selectedObject; public Material selectMat; private Material originalMat;
@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?