Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

My Buttons Don't Work!!!

Discussion in 'Scripting' started by R-ty_dragon1, Jun 17, 2022.

  1. R-ty_dragon1

    R-ty_dragon1

    Joined:
    Aug 3, 2021
    Posts:
    50
    My Button Doesn't Work.
    The function doesn't show up in the on click thing
    upload_2022-6-17_11-46-34.png
    My code is as follows
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class DetectIfParked : MonoBehaviour
    7. {
    8.     [SerializeField] RectTransform fader;
    9.     public GameObject level1Passed;
    10.     bool isColliding = false;
    11.  
    12.     void Start()
    13.     {
    14.         level1Passed.SetActive(false);
    15.     }
    16.     void OnCollisionEnter(Collision col)
    17.     {
    18.         if (col.gameObject.tag == "Objective")
    19.         {
    20.             isColliding = true;
    21.             StartCoroutine(TimerStart());
    22.             Debug.Log("Started Coroutine");
    23.         }      
    24.     }
    25.  
    26.     void OnCollisionExit()
    27.     {
    28.         isColliding = false;
    29.         Debug.Log(isColliding);
    30.     }
    31.  
    32.     void EndCoroutine()
    33.     {
    34.         StopCoroutine(TimerStart());
    35.     }
    36.  
    37.     IEnumerator TimerStart()
    38.     {
    39.         yield return new WaitForSeconds(2);
    40.         Debug.Log(isColliding);
    41.         if(isColliding == true)
    42.         {
    43.             Level1Passed();
    44.         }
    45.         else
    46.         {
    47.             Debug.Log("Ending Coroutine");
    48.             EndCoroutine();
    49.         }
    50.     }
    51.  
    52.     public void Level1Passed()
    53.     {
    54.         level1Passed.SetActive(true);
    55.         Time.timeScale = 0f;
    56.     }
    57.  
    58.     public void LoadLevel2()
    59.     {
    60.         fader.gameObject.SetActive(true);
    61.         LeanTween.scale(fader, Vector3.zero, 0f);
    62.         LeanTween.scale(fader, new Vector3(1,1,1), 0.5f).setEase(LeanTweenType.easeInOutExpo).setOnComplete(() =>
    63.         {
    64.             Invoke("Level2", 0.5f);
    65.         });
    66.     }
    67.  
    68.     private void Level2()
    69.     {
    70.         SceneManager.LoadScene(3);
    71.     }
    72. }
    The Scene I need to load is Scene 3

    Thank You
     
  2. chrische5

    chrische5

    Joined:
    Oct 12, 2015
    Posts:
    52
    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
     
    Bunny83 likes this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,541
    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? :)
     
  4. R-ty_dragon1

    R-ty_dragon1

    Joined:
    Aug 3, 2021
    Posts:
    50
    Oh Thanks I'll try
     
  5. R-ty_dragon1

    R-ty_dragon1

    Joined:
    Aug 3, 2021
    Posts:
    50
    Ok thanks it's level 2 and index 3 as index 0 is menu, index 1 is settings screen and index 2 is level1
     
  6. chrische5

    chrische5

    Joined:
    Oct 12, 2015
    Posts:
    52
    Hello

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

    Christoph
     
  7. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,541
    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:

    Code (CSharp):
    1.  
    2. public const FirstLevelIndex = 1;
    3. public void SwitchToLevel(int aLevel)
    4. {
    5.     SceneManager.LoadScene(aLevel - FirstLevelIndex);
    6. }
    7.  
    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.