Sorry if this is too simple, but i've not been able to find an answer on this. Usually either the question is too complicated to answer or too simple to not know.
THIS IS PARTIALLY A UNITY VR/VRTK ISSUE (maybe...)
Anyways, my goal is to set up triggers for my Canvas to be able to start a video player and have it enable and appear in the scene when an object is being picked up. As of now, everything is in place except for my ability to trigger both those actions when the player interacts with the game object.
Within my script, I have:
public VideoPlayer videoPlayer;
public Canvas canvas;
public GameObject asset;
void Awake ()
{
videoPlayer.GetComponent<VideoPlayer>();
canvas.GetComponent<Canvas>();
}
private void Start()
{
videoPlayer.Pause();
canvas.enabled = !canvas.enabled;
}
Simple, but everything gets linked within unity and its alot of fun. My problem is that im trying to reference a script that the GameObject has on it so that I can use its triggers in order to enable the video player to do its thing. Theres a simple bool within the Interactible script attached to it that I think might work or maybe i can write my own, problem is i have no idea how to reference from the GameObject components. Maybe im just not understaning it but ive looked for a while and now I turn to you, the collective for help...
help :(
-Thanks to all in advance, there's more pieces to the project but i thought these would be the most relevant. Let me know if i missed something needed.
I don't really understand what is your final goal yet but those two lines
don't make much sense. You either have the references already (e.g. from the inspector) than you don't need to get the components again or you want to get them from a GameObject
In case you want the components attached to the same GameObect as the provided script instead use
Or in case you rather want to get the components from the
assetGameObject useThan you can add a public method to call it whenever "interacting" (however that looks like) with the GameObject
From your comment
I understand you somewhere have a "triggering event" lets say on a GameObject
ObjectAin a classTriggeringBehavioure.g.and you want a method
ReactToInteractionbe called on your canvas whenever the trigger is called.There are many ways to do so but I personally would prefere one of following:
Solution 1: direct call
If you "know" your references, either referencing in the inspector or have some form of finding them using e.g. Singleton or
FindObjectOfType,Find(by name) etc you could just directly call the method fromTriggeringBehaviour:Solution 2: event Action
You can implement custom events in the
TriggeringBehaviourby using e.g.Than you would have to register listeners to those events in your
CanvasBahviourAlternatively you can also make a static class for handling those events
With this way it is a bit easier to register and invoke the events:
in
TriggerBehaviourand in
CanvasBehaviourThis is very flexible and can also pass as many parameters you want/need (In this case none anyway). However you still have to somehow get the according reference to the correct component.
One big flaw is that registering and especially destroying something without unregistering can lead to errors and be quite frustrating.
Solution 3: UnityEvent (My personal favourite)
Alternatively you can also implement a
UnityEventinTriggeringBehaviourThe big advantage is that this
UnityEventwill be shown and configurable in the Inspector! (You'll recognize it from the UI Button onClick event)So you can just reference any other component from the inspector and don't have to register listeners at runtime (You still can do so via
triggerBahviour.OnTriggered.AddListener()).However
UnityEventis kind of limitted. E.g. it is not that simple to have different Methods called with different signatures that have multiple or non-standart parameters (such as custom class references).