Hi Im new to scripting and I don’t know much so please bear with me. Im making a game and I have a panel with text on it saying “game over”. I also have a screen in the room that plays a video of a countdown when the player enters a trigger.
I want the UI panel with the text saying game over to enable when the video finishes and the countdown reaches zero. Does anyone know a simple way of doing this. Thanks a lot
Use Time.deltaTime or Time.time depending on what you need, check unity documentation for a more specific description about those 2 (you will probably need Time.deltaTime anyway).
You basically could have a float variable “timer”, and in the update reduce this variable by the time. When timer is 0 you could make the UI panel appear. Or do the other way around, and have the variable timer += Time.deltaTime and if timer is higher than what you need, something happens…
When you start the video of countdown you start the coroutine. Something like this:
using UnityEngine;
using System.Collections;
public class WaitForSecondsExample : MonoBehaviour
{
Gameobject gameoverPanel;
Void StartVideo ()
{
//your code where you start your
video
StartCoroutine(EnableGameoverPanel ());
}
IEnumerator EnableGameoverPanel()
{
yield return new WaitForSeconds(115);//wait for 115 seconds (1:55minutes as your video)
gameoverPanel.SetActive(true);
}
Sorry for being annoying but this is what I currently have with the addition of your script. So basically its a screen that plays static and when the player walks up to it and enters the trigger it plays a second video by destroying the screen in front.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class destroy : MonoBehaviour {
public bool destroyOtherObject, showObject;
public GameObject objectToDestroy, objectToShow;
GameObject gameoverpanel;
void OnTriggerEnter (Collider other)
{
StartCoroutine(EnableGameoverPanel ());
}
IEnumerator EnableGameoverPanel()
{
yield return new WaitForSeconds(115);
gameoverpanel.SetActive(true);
}
{
if(other.tag == "Player") {
if (destroyOtherObject){
Destroy (objectToDestroy);
}
if (showObject) {
objectToShow.SetActive (true);
}
}
}
}
I’m probably being really stupid but do you know why I’m getting this error
You forgot void OnTriggerEnter, which should be added on 24th line.
EDIT : You should have only 1 OnTriggerEnter.
Why there is no if statement on the 15th line?
Add the proper if statements on the first OnTriggerEnter and you shouldnt’t have problems.
Okay so I basically want it so that when the player enters the trigger the countdown video starts playing and i also want the coroutine to start counting down when they enter the trigger. Im a complete noob how can I have both things happen when the player enters the trigger?
If you enter the trigger → the countdown video start playing AND a coroutine starts.
tranos already told you how to do it, just carefully read the code he provided you and adjust it to your needs.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class destroy : MonoBehaviour {
public bool destroyOtherObject, showObject;
public GameObject objectToDestroy, objectToShow;
GameObject gameoverpanel;
void OnTriggerEnter (Collider other)
{
if(other.tag == "Player") {
if (destroyOtherObject){
Destroy (objectToDestroy);
}
if (showObject) {
objectToShow.SetActive (true);
}
}
StartCoroutine(EnableGameoverPanel ());
}
IEnumerator EnableGameoverPanel()
{
yield return new WaitForSeconds(115);
gameoverpanel.SetActive(true);
}
}
Also some basics to get you started:
Classes on C# Read it all! but to give you a quick view have this:
public class Person
{
// Public Field
public string name;
// Constructor that takes no arguments. You wont be using constructors in Unity unleass you have a class that doesn't inherit from MonoBehaviour or any other Unity Type, instead you would use Start() and Awake()
//Constructors are called whenever you create a new instance of this class
public Person()
{
name = "unknown";
}
// Constructor that takes one argument.
// This is called "Constructor overloading" which means that you can create a "new Person()" either without passing a name or passing a name "new Person("James")
public Person(string nm)
{
name = nm;
}
// Method
public void SetName(string newName)
{
name = newName;
}
// Methods can also be overloaded
public void SetName(string firstName, string lastName)
{
name = firstName + ", " + lastName;
}
}
public class Class1 : Monobehaviour
{
public int publicField = 1;
private int privateField = 2;
public void Method()
{
int localVariable = 0;
publicField = localVariable;
}
//Cannot be seen or used by someone else outside of this class
private void OtherMethod()
{
//Can't access localVariable from Method(), it doesn't exist here
//Can access class fields
publicField = 5;
privateField = 3;
}
private void ThirdMethod()
{
// You can call your own methods without restrictions
Method();
OtherMethod();
}
}
public class Class2 : Monobehaviour
{
//public field of type Class1, set it on the editor by dragging a Class1 component to this field
public Class1 otherClass;
public void UsingClass1()
{
otherClass.publicField = 3; //You can set public class fields
// privateField can not be accessed from here
otherClass.Method(); // You can call public methods
// localVariable of Method(), however, doesn't exist here
//OtherMethod() and ThirdMethod can't be accessed from here
}
//Cannot be seen or used by someone else outside of this class
private void OtherMethod()
{
//Can't access localVariable, it doesn't exist here
//Can access class fields
}
private void ThirdMethod()
{
You can call your own methods without restrictions
Method();
OtherMethod;
}
}
public class Class2 : Monobehaviour
{
// This is a method
void Method1()
{
//Do things here
}
//This is nothing. It doesn't know to which method it belongs
{
// Code here
}
// You can't do this, there can't be two methods with the same name. They don't extend, just write it on the other one
void Method1()
{
// Code
}
// You can overload it however. They should be similar on purpose like transform.Rotate(Vector3) and Transform.Rotate(Quaternion)
void Method1(ParameterType parameterName)
{
// Code, now using the parameterName parameter
}
}