Button on win?
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
Answer by Cepheid · Apr 22, 2016 at 02:38 PM
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! :)
Answer by keckjoseph · Apr 22, 2016 at 02:57 PM
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!
Well, I'm afraid to say that there's nothing there that should be causing this error, I also copied and tested your code in a Unity scene to make sure and it works just fine.
Are you attaching the actual button itself into the NextStageButton field through the inspector? Also, you could try moving the NextStageButton.SetActive(false) call into the Awake() method and see if it makes any difference as it's called as soon as the scene loads rather than when the object is activated.
void Awake ()
{
NextStageButton.SetActive(false);
}
Otherwise, I'm afraid I'm stumped as to what the issue could be unfortunately.
Also, quick head's up. This is a comment and not an answer and therefore should be posted as a comment to either your original question or my answer.
That worked perfectly placing it Awake! You are the best and thank you for your help so very much @Cepheid !
Your answer
Follow this Question
Related Questions
identify arrayed game objects 2 Answers
When Time.timeScale go from 1 to 0 Unity destroy a gameobject 1 Answer
clone a game object and use it 0 Answers
The Object you want to instantiate is null. 1 Answer
Find all gameObjects in the scene with specific layer and attach a script to them runtime? 0 Answers