My Buttons Don't Work!!!

My Button Doesn’t Work.
The function doesn’t show up in the on click thing
8213052--1072173--upload_2022-6-17_11-46-34.png
My code is as follows

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

public class DetectIfParked : MonoBehaviour
{
    [SerializeField] RectTransform fader;
    public GameObject level1Passed;
    bool isColliding = false;

    void Start()
    {
        level1Passed.SetActive(false);
    }
    void OnCollisionEnter(Collision col)
    {
        if (col.gameObject.tag == "Objective")
        {
            isColliding = true;
            StartCoroutine(TimerStart());
            Debug.Log("Started Coroutine");
        }      
    }

    void OnCollisionExit()
    {
        isColliding = false;
        Debug.Log(isColliding);
    }

    void EndCoroutine()
    {
        StopCoroutine(TimerStart());
    }

    IEnumerator TimerStart()
    {
        yield return new WaitForSeconds(2);
        Debug.Log(isColliding);
        if(isColliding == true)
        {
            Level1Passed();
        }
        else
        {
            Debug.Log("Ending Coroutine");
            EndCoroutine();
        }
    }

    public void Level1Passed()
    {
        level1Passed.SetActive(true);
        Time.timeScale = 0f;
    }

    public void LoadLevel2()
    {
        fader.gameObject.SetActive(true);
        LeanTween.scale(fader, Vector3.zero, 0f);
        LeanTween.scale(fader, new Vector3(1,1,1), 0.5f).setEase(LeanTweenType.easeInOutExpo).setOnComplete(() =>
        {
            Invoke("Level2", 0.5f);
        });
    }

    private void Level2()
    {
        SceneManager.LoadScene(3);
    }
}

The Scene I need to load is Scene 3

Thank You

Hello

Which function? If you mean Level2…btw the name sounds weird, if you load a scene with index 3, than try to make it public.

Christoph

1 Like

Don’t you think it might be a good idea to make that method public if you intend to use it from outside the class? :slight_smile:

Oh Thanks I’ll try

Ok thanks it’s level 2 and index 3 as index 0 is menu, index 1 is settings screen and index 2 is level1

Hello

Dont use magic numbers. In this case i would load the scenes by name. But thats only my opinion.

Christoph

Well, you can actually specify up to one primitive argument in a UnityEvent callback. So you can name your method SwitchToLevel and pass in an integer value. Something like that:

public const FirstLevelIndex = 1;
public void SwitchToLevel(int aLevel)
{
    SceneManager.LoadScene(aLevel - FirstLevelIndex);
}

This would actually introduce the concept of a “level” and abstracts a way the concept of a scene.

As I said, in the OnClick handler you can actually select methods with up to 1 arguments and specify the value that should be passed inside the OnClick handler. You really don’t want to create a seperate method for each level.

ps: I would also recommend to stick to names rather than scene indices. If you need another special scene, for whatever reason, your game levels would start at a different index. Using a constant like I’ve shown does work, though it has other issues. If you want to remove a level because it may no longer fit the style or whatever, all other levels would of course change their index. Depending on the usecase this may be desired, but could cause issues in other situations.