I am very new to using Unity and I am surviving off tutorials from YouTube. As soon as my game is launched, a video is started using Video Player. I am hoping to make the video hidden after the video has finished playing to reveal my menu screen. I do have a script that I have used to hide the video player
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HideVideo : MonoBehaviour
{
public GameObject VideoPlayer;
public void HideVideoPlayer()
{
VideoPlayer.gameObject.SetActive(false);
}
}
The problem is, the closest I have gotten to actually hiding my video is by setting it to an onclick event via a button. How do I make the video player hide after the video is finished playing? Thanks.
Why not simply hide it, when it stops playing and put it in Update?
void Update() {
if (!(VideoPlayer.isPlaying)) {
VideoPlayer.gameObject.SetActive(false);
}
}
A complete script could be look like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HideVideo : MonoBehaviour
{
public GameObject VideoPlayer;
public bool isPlayerStarted = false;
void Update() {
if (isPlayerStarted == false && VideoPlayer.IsPlaying == true) {
// When the player is started, set this information
isPlayerStarted = true;
}
if (isPlayerStarted == true && VideoPlayer.isPlaying == false ) {
// Wehen the player stopped playing, hide it
VideoPlayer.gameObject.SetActive(false);
}
}
}
Related
I'm making a subway simulator, I want the StationBox to always have the Created tag after pressing a button, but it goes back to the previous value when the game is restarted, how can I solve this?
Update: I will have more than 1 station, I would like to make a universal script for all
using System;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class AddStation : MonoBehaviour
{
public InputField Input;
public Text StationName;
public GameObject Button;
public GameObject CreateStation;
public GameObject StationBox;
public void OnMouseDown(){
if (Input.text != ""){
CreateStation.SetActive(false);
StationName.text = Input.text.ToString();
StationBox.tag = "Created";
}
}
}
If I understand correctly, you are trying to find a way to save game data so as not to lose it when you restart the game.
To do this the most practical solution is PlayerPrefs.
You can find a lot of documentation online because it's a simple concept.
However for your problem you can do this:
public void OnMouseDown(){
if (Input.text != ""){
CreateStation.SetActive(false);
StationName.text = Input.text.ToString();
StationBox.tag = "Created”;
PlayerPrefs.SetInt(“Created”, 1);
}
}
void Start()
{
if (PlayerPrefs.HasKey(“Created”))
if (PlayerPrefs.GetInt(“Created”) == 1)
StationBox.tag = "Created”;
}
I am making a game where, if the catcher gets destroyed by the object, the game over screen is triggered. All that seems to occur is that there is a giant game over the screen at the beginning of when I play, while the game is running in the background. For some reason, the game does not seem to call in the game over screen only on collision.
This is the script I am using for my catcher, where it collides, disappears, and then the game over screen is set up to be triggered.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CatcherDestroy : MonoBehaviour
{
public GameOverScreen GameOverScreen;
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Catcher"))
{
Destroy(collision.gameObject);
GameOverScreen;
}
}
}
and this is the code for my GameOverScreen.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameOverScreen : MonoBehaviour
{
public Text pointsText;
public void Setup(int score)
{
gameObject.SetActive(true);
pointsText.text = "Score:" + score.ToString();
}
}
You are not calling anything when an object collides, you're just listing the reference of the object. You'll need to call the function that you have exposed. Without calling a method from the script reference, no code will be run. Edit your first snippet as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CatcherDestroy : MonoBehaviour
{
public GameOverScreen GameOverScreen;
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Catcher"))
{
Destroy(collision.gameObject);
GameOverScreen.Setup();
}
}
}
The other option would be to move the code in Setup to Awake or Start or OnEnable, then instead of calling the function in the collision, you just need to set it as active.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CatcherDestroy : MonoBehaviour
{
public GameOverScreen GameOverScreen;
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Catcher"))
{
Destroy(collision.gameObject);
GameOverScreen.score = theScore;
GameOverScreen.gameObject.SetActive(true);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameOverScreen : MonoBehaviour
{
public Text pointsText;
public int score;
private void OnEnable()
{
pointsText.text = "Score:" + score.ToString();
}
}
The one issue is you'll need to pass in the score parameter which I do not see in your script CatcherDestroy.
I have a problem that I can't solve by my self.
I have a Pause Button which need to activate pause panel on scene, but nothing work.
1. I have a public GO "Panel" attached in the inspector.
2.Inspector writes that: "There is no 'GameObject' attached to the "Panel" game object, but a script is trying to access it."
3.Script on always active GO.
4.At start Panel is Active.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class ButtonController : MonoBehaviour {
private Scene ActiveScene;
private GameController gm;
public GameObject panel;
// Use this for initialization
void Start ()
{
gm = GetComponent<GameController>();
ActiveScene = SceneManager.GetActiveScene();
panel.SetActive(false);
}
public void Pause()
{
Debug.Log("Pause");
panel.SetActive(true);
Time.timeScale = 0;
}
public void Menu()
{
SceneManager.LoadScene(0);
}
public void Restart()
{
SceneManager.LoadScene(ActiveScene.buildIndex);
}
public void Play()
{
Time.timeScale = 1;
panel.SetActive(false);
}
Glad if u can help!
I solved the problem: I attached the script twice.
I have followed a tutorial on Youtube (https://www.youtube.com/watch?v=_nRzoTzeyxU) on how to create a dialogue system for a game. Since my game is a platformer/RPG, I am currently attempting to adapt this system to where the player can walk up to an NPC and press the "Submit" button to access their dialogue, instead of clicking a button on the canvas/UI.
So far I have created an Interactable script that allows the player to detect if they are in the range of the invisible sphere collider that is equipped to the NPC, which is working. If I try to access the dialogueTrigger script that is equipped to the NPC however, I get a NullReferenceException error. I would like some help on how to properly call the dialogueTrigger script from the NPC and trigger the dialogue event, as I am very new to code and I only have this so far. Any help would be appreciated.
EDIT:
NullReferenceException: Object reference not set to an instance of an object
Interactable.Update () (at Assets/Scripts/Interactable.cs:34)
Dialogue Trigger is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DialogueTrigger : MonoBehaviour
{
public Dialogue dialogue;
public void TriggerDialogue ()
{
FindObjectOfType<DialogueManager>().StartDialogue(dialogue);
}
}
Interactable is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Interactable : MonoBehaviour
{
private GameObject triggeringNpc;
private bool triggering;
public DialogueTrigger Diag;
void Start()
{
}
void Update()
{
if(triggering)
{
Debug.Log("Within Range");
if (Input.GetButtonDown("Submit"))
{
Debug.Log("Pressed the Interact Button");
Diag.TriggerDialogue();
}
}
}
void OnTriggerEnter(Collider other)
{
if(other.tag == "NPC")
{
triggering = true;
triggeringNpc = other.gameObject;
}
}
void OnTriggerExit(Collider other)
{
if(other.tag == "NPC")
{
triggering = false;
triggeringNpc = null;
}
}
}
The culprit would be your Diag reference. Have you correctly dragged a DialogueTrigger prefab from your Hierarchy window and into the public field on Interactable?
How do I set gameobject(myCanvas) to be false during the first play?
I've put this script into the cube, and when I click the cube it will show the Canvas.
I haven't been clicking on the cube but, the canvas has already come out.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class MouseDownText : MonoBehaviour {
public Canvas myCanvas;
void Start()
{
// first start game. gameobject will turn off
myCanvas.gameObject.SetActive(false);
}
void OnMouseDown()
{
// for switch on/off
myCanvas.gameObject.SetActive(!myCanvas.gameObject.activeSelf);
}
}
From what I understand you have a canvas component attached to a game object (a cube) and you want to use mouse clicks to toggle whether or not the canvas is active.
Have you tried:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class MouseDownText : MonoBehaviour {
public Canvas myCanvas;
// Use this for initialization
void Start () {
myCanvas = GetComponent<Canvas> ();
myCanvas.enabled = false;
}
// Update is called once per frame
void Update () {
}
void OnMouseDown()
{
// for switch on/off
if (myCanvas.enabled)
myCanvas.enabled = false;
else
myCanvas.enabled = true;
}
}
Also make sure you haven't accidentally enabled it in some other code somewhere.