Hello,
New to Unity/C#… (1.5 weeks)
13 years html
8 years php/mysql
I’m trying a simple command in unity 5.3 to have a button show up after you win a level or stage (a scene).
I’m just updating the Rollaball tutorial (to get a feel for the engine and language) and I can’t seem to figure it out…after about 4 hours of trying and searching the forums I figured I’d ask for help…
Here’s what I have…
Scene 0 - Title Screen
I have a button that when you hit it it brings you to scene 1 (level 1).
Scene 1 - Level 1
The Tutorial game with a few changes, the end game saying “You Win!” and from there I am trying to have a button “Next Stage” (I already have the script and have tested it to bring you to the next scene,
But I can only get it to sit on screen…I have made it a prefab (and deleted it from Hierarchy so it doesn’t just sit on screen) and assigned it in the Player Controller but when the you win shows up the button does not…
This is what I have…I’m sure it’s something very simple and my newness is just hindering me from the obvious.
public GameObject NextStageButtonPrefab;
{
WinText.text = "You Win!";
Instantiate(NextStageButtonPrefab, transform.position, Quaternion.identity);
}
As you can see I’m trying to Instantiate it once you win the level…the button just never appears…I have tried to do this 100 different ways and this just seems the best, yet still incorrect way…
Any help is greatly appreciated and if there is simply and easier way that would also be helpful.
Thank You,
Joseph
Hi there @keckjoseph
I’m assuming you’re using a button from the new UI system? In which case, you do not actually need to instantiate a new instance of the button, you can in fact de-activate the button and then simply activate it once the end game condition has passed. This way, the button stay’s at it’s original pixel coordinates and retains it’s anchor points and simply appears on the screen. Consult the following:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ExampleClass : MonoBehaviour
{
public Text WinText;
public GameObject NextStageButton;
void Start ()
{
// De-activate the button at the start of the level.
NextStageButton.SetActive(false);
}
// Example method for your game over logic.
void GameComplete ()
{
WinText.text = "You Win!";
// Re-activate the button now that the player has won.
NextStageButton.SetActive(true);
}
}
Now, you can create the UI button and assign it’s necessary events and logic and then simply disable it until the player wins, at which point the button appears again and the player can click it to continue.
I hope this helps!
Thank You so much @Cepheid. while I was waiting for a reply this is exactly what I was looking into…I am now 90% there thanks to you help…for some reason the button will NOT deactivate at play…even with the
NextStageButton.SetActive(false);
but, here’s the kicker if I uncheck it in unity, play the level, at the end of the level it will Active and show up. So essentially it is working, but not without question as to why it wont de-activate at start of scene… I was wondering if you could take a quick peek at the code, see if your experience is seeing something I’m missing…and thank you again so much because like I said it is doing what I need it to do, but I’m just not understanding why it’s active at start now.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.SceneManagement;
public class PlayerController : MonoBehaviour
{
public float speed;
public Text CountText;
public Text WinText;
public Text TimerText;
public float timeRemaining;
public GameObject NextStageButton;
private Rigidbody rb;
private int count;
void Start()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText();
WinText.text = "";
TimerText.text = "";
NextStageButton.SetActive(false);
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
}
void Update ()
{
timeRemaining -= Time.deltaTime;
if (timeRemaining > 0)
{
TimerText.text = "TIME: " + (int)timeRemaining;
}
else
{
TimerText.text = "TIME'S UP!";
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
other.gameObject.SetActive(false);
count = count + 1;
SetCountText ();
if (count >= 8)
{
WinText.text = "You Win!";
NextStageButton.SetActive(true);
}
}
}
void SetCountText()
{
CountText.text = "Count: " + count.ToString();
}
}
Thank you again so much for you help…what u did there already is very much appreciated!