- Home /
The question is answered, right answer was accepted
Why is the Winning text appearing after starting the game?
In my animation, after destroying all objects, appears texts: WinningImage, ExitButton, NextLevelButton appear.
I do not understand why: WinningImage, ExitButton, NextLevelButton;, appear immediately after pressing PLAY, while it should be turned off until the animation is won.
Could someone find a mistake?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Gamer : MonoBehaviour {
[SerializeField]
Camera camMain; // drag n drop in the inspector
int index =1;
public GameObject ImageOver, restartButton , menuButton;
public GameObject WinningImage, ExitButton, NextLevelButton ;
public GameObject Manager;
public void OnCollisionEnter2D (Collision2D col)
{
Debug.Log("Player hit object: " + col.transform.name);
//check if the object you hit is one of the "collisionObjects":
collisionObject colObj= col.transform.GetComponent<collisionObject>();
if(!colObj) return;
CheckDestroyedIndex(colObj);
}
void CheckDestroyedIndex(collisionObject cObj) {
if(index == cObj.myIndexNumber)
{
if (cObj.isHighestNumber)
{
Debug.Log ("Yay you correctly hit the last Square, You WIN!");
WinningImage.SetActive (true);
restartButton.SetActive (true);
gameObject.SetActive (false);
Manager.SetActive (false);
}
Destroy(cObj.gameObject);
index++;
}
// here, we know it didn't match the number. :)
else
{
ImageOver.SetActive (true);
restartButton.SetActive (true);
menuButton.SetActive (true);
gameObject.SetActive (false);
Manager.SetActive (false);
WinningImage.SetActive (false);
}
}
void Update() {
if(Input.GetMouseButtonDown(0)) {
RaycastHit2D hit = Physics2D.Raycast(camMain.ScreenToWorldPoint(Input.mousePosition), Vector3.zero);
if(hit) {
collisionObject cObj = hit.collider.GetComponent<collisionObject>();
if(cObj) CheckDestroyedIndex(cObj);
}
}
}
}
Answer by polan31 · May 14, 2018 at 06:39 PM
The problem is solved. I added Awake and everything works.
Answer by ImpOfThePerverse · May 14, 2018 at 06:20 PM
Are you seeing the "Player hit object..." and "Yay you correctly hit..." debug text lines at start? If not, you might be forgetting to disable the buttons before pressing play. If you are seeing those lines, it might help you to know which collider is triggering the collision.
Follow this Question
Related Questions
I want to make text appear for a few seconds and then disappear. 2 Answers
How to get a 3D Text to appear on collision of another object? 2 Answers
Gui text script - help 1 Answer
[SOLVED] Why is text not appearing? 2 Answers
Text shows on Even numbers, then another text shows on Odd numbers. 0 Answers