Home>

I want the button to proceed to the next in Unity to appear when the game clear is displayed in the UI
Please write the details of your question here.
(Example) I am making a ●● system with PHP (CakePHP).
■■ The following error message occurred while implementing the function.

There is no particular error

Error message
Corresponding source code
Source code


public GameObject button;

void Start ()
{
// hitPoint = hitPointMax;
button.SetActive (false);
}

private void OnTriggerEnter2D (Collider2D collision)
{

if (hitPoint<= 0)
{

button.SetActive (true);

}

}

Thanks for your answer. No way to come so much!

Well, it was selfish. I've added it, so please ...

Just debug
twenty four
UnityEngine.Debug: Log (Object)
EnemyAP: OnTriggerEnter2D (Collider2D) (at Assets/Scripts/EnemyAP.cs: 40)
I think that HP is decreasing because it came out.

I'm sorry, there was something like a yellow error instead of a red error.

Assets \ Scripts \ EnemyAP.cs (12,9): warning CS0414: The field'EnemyAP.damage' is assigned but its value is never used

Lines 12.9 are int damage = -1;
And public int hitPoint = 45 ;.

Thanks for your patience.

public class EnemyAP: MonoBehaviour
{
public int hitPoint = 45;
public int hitPointMax = 45;

int damage = -1;

public Text Score text;

public Text Gameclear text;

// Update is called once per frame
void Update ()
{
Scoretext.text = hitPoint.ToString ();

if (hitPoint<= 0)
{
button.SetActive (true);
}
private void OnTriggerEnter2D (Collider2D collision)
{
if (collision.gameObject.tag == "Buster")
{
hitPoint-= 1;
Debug.Log (hitPoint);
hitPoint = Mathf.Clamp (hitPoint, 0, hitPointMax);
}

if (hitPoint<= 0)
{

Destroy (gameObject);
// Instantiate (m_explosionPrefab, collision.transform.localPosition, Quaternion.identity);
//GameObject.Find ("Canvass"). GetComponent(). GameOver ();
button.SetActive (true);

}

}
}

What I tried

Please describe here what you have tried for the problem.
The code is only the main points in consideration of being squeezed.

As a situation, set HP in the script of the enemy object, and if HP is below the standard value, it will be Destroyed.

HP is also displaying it in the UI, and I am using it by pulling the process of subtracting the numerical notation of the UI from the script of this AP.

Supplementary information (FW/tool version, etc.)

Please provide more detailed information here.
I'm new to Unity, so it may be really rudimentary.

Is it possible to attach an object called a button with public GameObject button ;?

I was able to attach the button object from the inspector, and since the button is hidden when the game starts, I can only display it, but I would like to ask you to teach me, so I used this for the first time. ..

  • Answer # 1

    Possible causes
    buttonIs not assigned(This is not because it is assigned publicly)
    OnTriggerEnter2DIs not called
    hitPointDoes not fall below 0
    ・ The button is hidden in the part not written in the question (high possibility)

    Also, you said, "The code is only the main point considering that it will be cracked", but since you do not know the cause of this problem, you will not know what you can understand if you worry about it.

  • Answer # 2

    I got the object with Find ~ and it was done. Thank you very much!