Unity 3d script adding functions as trigger on Inspector window - c#

I have defined three functions (OnReticleEnter(),Exit() and hover()) in the script and attached with a game object:
using UnityEngine;
using System.Collections;
using TMPro;
public class MyObject : MonoBehaviour
{
// Custom reticle events
public GameObject gOBJ;
void OnReticleEnter()
{
gOBJ.SetActive(true);
}
void OnReticleExit()
{
gOBJ.SetActive(false);
}
void OnReticleHover()
{
gOBJ.SetActive(true);
}
}
My problem is I want these functions in the inspector window so that I can define multiple actions like event trigger's pointer enter.
Is there any way I can make these functions available as event trigger on inspector window?

Related

unity wont let me attatch object to variable

Unity wont let me drag and drop a game object into the variable
heres my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions;
public class GameManager : Singleton<GameManager>
{
[SerializeField] private Player currentPlayer;
public Player CurrentPlayer
{
get { return currentPlayer; }
}
private void Awake()
{
Assert.IsNotNull(currentPlayer);
}
}
Your public class GameManager : Singleton<GameManager> should be a monobehaviour.
Monobehaviours are the type to inherit from when you to add functionality to a gameObject. You cannot drag whatever to a script that just hold a [SerializeField].
You need to have a Monobehaviour inherited class as a component of a gameobject (meaning that it should be attached to it). There is where you can drag the componenets into the public or the private [SerializeField] fields shown in the editor when you select the correspoding gameobject.
Your script must inherit from Monobehavior for you to be able to do that. And since Singleton is a generic type, that wont work.

How to use "OnClick.AddListener" on multiple GameObjects in one Frame

I'm pretty new to Unity and C# scripting.
In my scene I have four buttons. I declared them in my script as public and put the items (on type Button) via drag and drop in my inspector in unity. Depending on which button is clicked, i want my program to do something. You can see in the script below what I've done. That scripts works perfectly just like I want it to.
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Example : MonoBehaviour
{
public Button Left;
public Button Right;
public Button Starten;
public Button Zurueck;
private void Start()
{
loadRezept(0);
Right.onClick.AddListener(iterationUp);
Left.onClick.AddListener(iterationDown);
Starten.onClick.AddListener(starten);
Zurueck.onClick.AddListener(zurueck);
}
... (here are the methods "iterationUp" and so on...)
This script work perfectly. But now there is my question: how can I do the same on using gameobjects?
If I just change the type "Button" to "GameObject" I don't have the possibility to use "onClick.AddListener". I don't want a script for every single gameobject. I just want something like I posted above.
GameObject itself has no such events.
Sounds like what you are asking for is a custom UnityEvent
You can create your own component like e.g.
public class HighlightController : MonoBehaviour
{
public UnityEvent OnHighlight;
// Somewhere Invoke the event e.g.
public void SetHighlighted()
{
OnHighlight.Invoke();
}
}
where you can add listeners now either via code or via the Inspector (just like the onClicked).
So put this on your GameObjects and then e.g. have fields like
[SerializeField] private HighlightController highlight;
//etc...
private void Awake()
{
highlight.OnHighlight.AddListener(HandleHighlight);
//etc...
}
private void HandleHighlight ()
{
...
}
// Make sure to cleanup listeners when not needed anymore
private void OnDestroy()
{
highlight.OnHighlight.RemoveListener(HandleHighlight);
//etc...
}
Or yes as mentioned as simple event could do the same without the serialization:
public class HighlightController : MonoBehaviour
{
public event Action OnHighlight;
public void SetHighlighted ()
{
OnHighlight?.Invoke();
}
}
and do
private void Awake()
{
highlight.OnHighlight += HandleHighlight;
}
private void OnDestroy ()
{
highlight.OnHighlight -= HandleHighlight;
}

Control video player with button and change scene

I'm trying to create a FMV game and I would like to figure out a way to control the video player with a UI button to change scene but only after the clip has finished playing.
If the user doesn't pick anything it will automatically be a default pathway and I have managed to use loopPoinReached and it has worked.
I can figure out how to use the same loopPointReached with a button.
Here is the video player code and it will auto load the next scene.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class loadBaseCampSeq : MonoBehaviour
{
void Start()
{
GameObject camera = GameObject.Find("Main Camera");
var videoPlayer = camera.AddComponent<UnityEngine.Video.VideoPlayer>();
videoPlayer.renderMode = UnityEngine.Video.VideoRenderMode.CameraNearPlane;
videoPlayer.url = "Assets/VideoAssets/intro.mp4";
videoPlayer.isLooping = false;
videoPlayer.Play();
videoPlayer.loopPointReached += EndReached;
}
void EndReached(UnityEngine.Video.VideoPlayer vp)
{
UnityEngine.SceneManagement.SceneManager.LoadScene("baseCampSeq");
}
}
Here is the basic code that I have assigned to the UI Button On Click ()
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class loadTruckButton : MonoBehaviour
{
public void truckSeq()
{
SceneManager.LoadScene("truckSeq");
}
}
I have been trying to reference the "EndReached" from the other script in the button but I cannot figure it out.
Any help would be awesome!
Thanks
You'll have to segregate the videoplayer functionality from the loadBaseCampSeq class into a new class. In the loadBaseCampSeq class create a videoplayer (instance) field and an abstract EndReached method:
public abstract class loadBaseCampSeq
{
public VideoPlayer videoPlayer;
public abstract void EndReached();
}
Create a button class which inherits from the loadBaseCampSeq base class. In the button class override the EndReached method that uses the videoplayer instance from it's base class:
public class Button : loadBaseCampSeq
{
public override class EndReached()
{
videoPlayer.EndReached();
}
}

How can we display an image when the player collide with an object?

I'm working on 2D Unity game and I would like to know how we can display an image on my inventory when the player collide with an object. For exemple, if the player collide with a drumstick, the drumstick image appear on the inventory menu.
Best Regards.
Yacine TAZDAIT.
Method #1
There are many different ways to display an image. For example, you can have an image with an image component, and you switch the component on and off whenever you want the image to appear/disappear. You can do this using code similar to this:
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class Example : MonoBehaviour
{
public Image drumstick;
public void Start()
{
toggleDrumstick(); // This will toggle the drumstick. For example, if the drumstick is not being shown at the time, the drumstick will show on the screen. The opposite is true.
}
public void toggleDrumstick() {
drumstick.enabled = !drumstick.enabled;
}
}
Method #2
The code above is a great solution, but there is a more modular way to do it.
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class Drumstick : MonoBehaviour
{
public static bool enabled = this.image.enabled;
}
I recommend the method above. The reason for this is because every script can now access the drumstick's status. For example, your player script can do this.
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
void doSomething () {
Drumstick.enabled = true; // make the image appear.
}
}
For any of these methods to work, make sure your drumstick uses the image component.
EDIT:
To answer your question further, here is a way to implement method #2 in your code. In your player script, you can use OnCollisionEnter and the method above to make the drumstick appear.
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
void OnCollisionEnter (Collision collision)
{
if (collision.gameObject.tag == "drumstick") Drumstick.enabled = false;
}
}
For this to work, make sure that the drumstick has the tag "drumstick".

Mimicking custom function

I have defined some custom functions in the following script and attached with my gameobject. I want to declare my function OnReticleEnter() to mimick OnPointerEnter(). Is it possible? I want this because I want to access my function OnReticleEnter() in the inspector so that I can define multiple actions like EventTrigger's Point Enter, Exit etc.
Is it possible to make the function OnReticleEnter() to behave as OnPointerEnter() programmatically?
using UnityEngine;
using System.Collections;
public class MyObject : MonoBehaviour
{
// Custom reticle events
public GameObject gOBJ;
public void OnReticleEnter()
{
gOBJ.SetActive(true);
}
public void OnReticleExit()
{
gOBJ.SetActive(false);
}
public void OnReticleHover()
{
gOBJ.SetActive(true);
}
}
You need to inherit the interfaces for the pointerEnter and PointerExit events in your class to be able to implement them. Your code might look something like this :
using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
public class MyObject : MonoBehaviour,IPointerEnterHandler, IPointerExitHandler
{
// Custom reticle events
public GameObject gOBJ;
public void OnPointerEnter(PointerEventData eventData){
gOBJ.SetActive(true);
}
public void OnPointerExit(PointerEventData eventData){
gOBJ.SetActive(false);
}
}

Categories

Resources