Home > Blockchain >  How to destroy Button in unity
How to destroy Button in unity

Time:09-22

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ScriptToVisibleButton : MonoBehaviour
{

public NextLevelTeleportScript LvlEnd;
public Button button;

void Start()
{

}

void Update()
{
    if (LvlEnd = 1)
    {
        button.gameObject.SetActive(false);
    }
    else
    {
        button.gameObject.SetActive(true);
    }

}
}

/////////////

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class NextLevelTeleportScript : MonoBehaviour
{

public int LvlEnd = 0;

void Start()
{

}

void OnTriggerEnter(Collider other)
{
    SceneManager.LoadScene(1);
    LvlEnd = 1;
}

void Update()
{

}

/////////// in this link is screen shot of error [1]: https://i.stack.imgur.com/Up9ru.png

can someone answer me quickly. I'm in a hurry. This script is supposed to work so that when I change the scene I get a button to the next level. (1 is true and 0 is false)

CodePudding user response:

It looks like that error is due to your if statement:

if (LvlEnd = 1)

Try:

if (LvlEnd.LvlEnd == 1)

instead.

CodePudding user response:

As @Rrrrrr said, your if is wrong and the other thing I noticed is that you are referencing the script with the variable name in it, but you haven't actually accessed the variable. The right would be:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ScriptToVisibleButton : MonoBehaviour
{

public NextLevelTeleportScript nextLevelTeleportScript; //just reference to script
public Button button;

void Start()
{
//You will probably need to reference the script either in the inspector, or around here. 
}

void Update()
{
    if (nextLevelTeleportScript.LvlEnd == 1) //now you get your variable and use
    {
        button.gameObject.SetActive(false);
    }
    else
    {
        button.gameObject.SetActive(true);
    }

}
}

I think this will work!

  • Related