
It looks like you're new here. If you want to get involved, click one of these buttons!
Hi,
I am making a game based off the How To Make A Game Video series on YouTube, and instead of having the game go automatously to the next level (Like in the video), I would like it to show a Level Complete Screen. (Same thing with crashing/falling off the edge, I want a Fail screen instead of just restarting the level.) Right now I have a script (That works) saying when the player collides with the finish line, the message "Level Complete!" is put into the console. How can I also add code so that a UI Canvas/Text/Button also appears? I am brand new to coding, and have spent 2+ hours searching the internet for a solution without success.
Please help if you can,
Greenreader9
Current Code:
using UnityEngine;
using UnityEngine.UI;
public class EndGame : MonoBehaviour
{
public void OnCollisionEnter(Collision col)
{
if (col.gameObject.name == "Player")
{
Debug.Log("Level Complete!");
}
}
}
This script is attached to the Finish Line and works properly.
Let me know if you have a solution!
if you attached your script to CanvasWin gameobject in the editor, you should use ''gameObject.setActive(true)" instead of "CanvasWin.setActive(true)" in the code.
but if you didn't attach it: first you should define a variable in the script that points to the CanvasWin then attach CanvasWin game object to the script in the editor finally you can use "CanvasWin.setActive()".
but I recommend the first way.
Just do:
public GameObject CanvasWin;
at the top (just above
public void OnCollisionEnter(Collision col)
and assign the CanvasWin reference in unity Inspector.
Answers
you have to design your canvas(including text or button ...) and hide it by default.
then in the script and in the if statement that you wrote in the code ( if (col.game.....)) show it by using setActive() function.
Thanks @shaniJf for the quick response. As I mentioned, I am new to coding, so would I do something like what is below?
Thanks,
Greenreader9
(CanvasWin is the name of the Canvas I want to appear.)
if (col.gameObject.name == "Player")
{
Debug.Log("Level Complete!");
setActive().CanvasWin;
}
CanvasWin.setActive(true) //for showing canvas
canvasWin.setActive(False) //for hiding canvas
Thanks @shaniJf for the responce.
I implemented what you suggested, but I unfortunately got an error message
"Assets\EndGame.cs(13,13): error CS0103: The name 'CanvasWin' does not exist in the current context"
My GameObject is called "CanvasWin" and my script is attached to the finish line. Can you please let me know what is going on? Thanks, Greenreader9
using UnityEngine;
using UnityEngine.UI;
public class EndGame : MonoBehaviour
{
public void OnCollisionEnter(Collision col)
{
if (col.gameObject.name == "Player")
{
Debug.Log("Level Complete!");
CanvasWin.setActive(true);
}
}
}