Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question For some reason the function of the script dies after one scene.

Discussion in 'Scripting' started by justinmed, May 21, 2024.

  1. justinmed

    justinmed

    Joined:
    Dec 19, 2021
    Posts:
    106
    For some reason the function of this script dies on scene change and never works again until I restart the editor and after the first scene it dies again. Can anyone see what might be going on here?


    It is for a piano app as you might notice and the pattern just continues to go down the keys which I excluded most of but left enough that one should get the jist. I am not plugging any of the notes into the script manually as you can see.

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using FullscreenEditor;
    5. using Minis;
    6. using UnityEngine;
    7. using UnityEngine.EventSystems;
    8. using UnityEngine.InputSystem;
    9. using UnityEngine.UI;
    10.  
    11.  
    12.  
    13. public class MIDI_InputTest : MonoBehaviour
    14. {
    15.  
    16.     public Button A0Button;
    17.     public Button A1Button;
    18.     public Button A2Button;
    19.     public Button A3Button;
    20.     public Button A4Button;
    21.     public Button A5Button;
    22.     public Button A6Button;
    23.     public Button A7Button;
    24.     public Button Ab1Button;
    25.     public Button Ab2Button;
    26.     public Button Ab3Button;
    27.     public Button Ab4Button;
    28.     public Button Ab5Button;
    29.     public Button Ab6Button;
    30.     public Button Ab7Button;
    31.     public Button B0Button;
    32.     public Button B1Button;
    33.     public Button B2Button;
    34.     public Button B3Button;
    35.     public Button B4Button;
    36.     public Button B5Button;
    37.     public Button B6Button;
    38.     public Button B7Button;
    39.     public Button Bb0Button;
    40.     public Button Bb1Button;
    41.     public Button Bb2Button;
    42.     public Button Bb3Button;
    43.     public Button Bb4Button;
    44.     public Button Bb5Button;
    45.     public Button Bb6Button;
    46.     public Button Bb7Button;
    47.     public Button C1Button;
    48.     public Button C2Button;
    49.     public Button C3Button;
    50.     public Button C4Button;
    51.     public Button C5Button;
    52.     public Button C6Button;
    53.     public Button C7Button;
    54.     public Button C8Button;
    55.     public Button D1Button;
    56.     public Button D2Button;
    57.     public Button D3Button;
    58.     public Button D4Button;
    59.     public Button D5Button;
    60.     public Button D6Button;
    61.     public Button D7Button;
    62.     public Button Db1Button;
    63.     public Button Db2Button;
    64.     public Button Db3Button;
    65.     public Button Db4Button;
    66.     public Button Db5Button;
    67.     public Button Db6Button;
    68.     public Button Db7Button;
    69.     public Button E1Button;
    70.     public Button E2Button;
    71.     public Button E3Button;
    72.     public Button E4Button;
    73.     public Button E5Button;
    74.     public Button E6Button;
    75.     public Button E7Button;
    76.     public Button Eb1Button;
    77.     public Button Eb2Button;
    78.     public Button Eb3Button;
    79.     public Button Eb4Button;
    80.     public Button Eb5Button;
    81.     public Button Eb6Button;
    82.     public Button Eb7Button;
    83.     public Button F1Button;
    84.     public Button F2Button;
    85.     public Button F3Button;
    86.     public Button F4Button;
    87.     public Button F5Button;
    88.     public Button F6Button;
    89.     public Button F7Button;
    90.     public Button G1Button;
    91.     public Button G2Button;
    92.     public Button G3Button;
    93.     public Button G4Button;
    94.     public Button G5Button;
    95.     public Button G6Button;
    96.     public Button G7Button;
    97.     public Button Gb1Button;
    98.     public Button Gb2Button;
    99.     public Button Gb3Button;
    100.     public Button Gb4Button;
    101.     public Button Gb5Button;
    102.     public Button Gb6Button;
    103.     public Button Gb7Button;
    104.  
    105.  
    106.    
    107.  
    108.     void Start()
    109.    
    110.    
    111.     {
    112.         A1Button = GameObject.Find("A1Button").GetComponent<Button>();
    113.         A0Button = GameObject.Find("A0Button").GetComponent<Button>();
    114.         A2Button = GameObject.Find("A2Button").GetComponent<Button>();
    115.         A3Button = GameObject.Find("A3Button").GetComponent<Button>();
    116.         A4Button = GameObject.Find("A4Button").GetComponent<Button>();
    117.         A5Button = GameObject.Find("A5Button").GetComponent<Button>();
    118.         A6Button = GameObject.Find("A6Button").GetComponent<Button>();
    119.         A7Button = GameObject.Find("A7Button").GetComponent<Button>();
    120.         Ab1Button = GameObject.Find("Ab1Button").GetComponent<Button>();
    121.         Ab2Button = GameObject.Find("Ab2Button").GetComponent<Button>();
    122.         Ab3Button = GameObject.Find("Ab3Button").GetComponent<Button>();
    123.         Ab4Button = GameObject.Find("Ab4Button").GetComponent<Button>();
    124.         Ab5Button = GameObject.Find("Ab5Button").GetComponent<Button>();
    125.         Ab6Button = GameObject.Find("Ab6Button").GetComponent<Button>();
    126.         Ab7Button = GameObject.Find("Ab7Button").GetComponent<Button>();
    127.         B0Button = GameObject.Find("B0Button").GetComponent<Button>();
    128.         B1Button = GameObject.Find("B1Button").GetComponent<Button>();
    129.         B2Button = GameObject.Find("B2Button").GetComponent<Button>();
    130.         B3Button = GameObject.Find("B3Button").GetComponent<Button>();
    131.         B4Button = GameObject.Find("B4Button").GetComponent<Button>();
    132.         B5Button = GameObject.Find("B5Button").GetComponent<Button>();
    133.         B6Button = GameObject.Find("B6Button").GetComponent<Button>();
    134.         B7Button = GameObject.Find("B7Button").GetComponent<Button>();
    135.         Bb0Button = GameObject.Find("Bb0Button").GetComponent<Button>();
    136.         Bb1Button = GameObject.Find("Bb1Button").GetComponent<Button>();
    137.         Bb2Button = GameObject.Find("Bb2Button").GetComponent<Button>();
    138.         Bb3Button = GameObject.Find("Bb3Button").GetComponent<Button>();
    139.         Bb4Button = GameObject.Find("Bb4Button").GetComponent<Button>();
    140.         Bb5Button = GameObject.Find("Bb5Button").GetComponent<Button>();
    141.         Bb6Button = GameObject.Find("Bb6Button").GetComponent<Button>();
    142.         Bb7Button = GameObject.Find("Bb7Button").GetComponent<Button>();
    143.         C1Button = GameObject.Find("C1Button").GetComponent<Button>();
    144.         C2Button = GameObject.Find("C2Button").GetComponent<Button>();
    145.         C3Button = GameObject.Find("C3Button").GetComponent<Button>();
    146.         C4Button = GameObject.Find("C4Button").GetComponent<Button>();
    147.         C5Button = GameObject.Find("C5Button").GetComponent<Button>();
    148.         C6Button = GameObject.Find("C6Button").GetComponent<Button>();
    149.         C7Button = GameObject.Find("C7Button").GetComponent<Button>();
    150.         C8Button = GameObject.Find("C8Button").GetComponent<Button>();
    151.         D1Button = GameObject.Find("D1Button").GetComponent<Button>();
    152.         D2Button = GameObject.Find("D2Button").GetComponent<Button>();
    153.         D3Button = GameObject.Find("D3Button").GetComponent<Button>();
    154.         D4Button = GameObject.Find("D4Button").GetComponent<Button>();
    155.         D5Button = GameObject.Find("D5Button").GetComponent<Button>();
    156.         D6Button = GameObject.Find("D6Button").GetComponent<Button>();
    157.         D7Button = GameObject.Find("D7Button").GetComponent<Button>();
    158.         Db1Button = GameObject.Find("Db1Button").GetComponent<Button>();
    159.         Db2Button = GameObject.Find("Db2Button").GetComponent<Button>();
    160.         Db3Button = GameObject.Find("Db3Button").GetComponent<Button>();
    161.         Db4Button = GameObject.Find("Db4Button").GetComponent<Button>();
    162.         Db5Button = GameObject.Find("Db5Button").GetComponent<Button>();
    163.         Db6Button = GameObject.Find("Db6Button").GetComponent<Button>();
    164.         Db7Button = GameObject.Find("Db7Button").GetComponent<Button>();
    165.         E1Button = GameObject.Find("E1Button").GetComponent<Button>();
    166.         E2Button = GameObject.Find("E2Button").GetComponent<Button>();
    167.         E3Button = GameObject.Find("E3Button").GetComponent<Button>();
    168.         E4Button = GameObject.Find("E4Button").GetComponent<Button>();
    169.         E5Button = GameObject.Find("E5Button").GetComponent<Button>();
    170.         E6Button = GameObject.Find("E6Button").GetComponent<Button>();
    171.         E7Button = GameObject.Find("E7Button").GetComponent<Button>();
    172.         Eb1Button = GameObject.Find("Eb1Button").GetComponent<Button>();
    173.         Eb2Button = GameObject.Find("Eb2Button").GetComponent<Button>();
    174.         Eb3Button = GameObject.Find("Eb3Button").GetComponent<Button>();
    175.         Eb4Button = GameObject.Find("Eb4Button").GetComponent<Button>();
    176.         Eb5Button = GameObject.Find("Eb5Button").GetComponent<Button>();
    177.         Eb6Button = GameObject.Find("Eb6Button").GetComponent<Button>();
    178.         Eb7Button = GameObject.Find("Eb7Button").GetComponent<Button>();
    179.         F1Button = GameObject.Find("F1Button").GetComponent<Button>();
    180.         F2Button = GameObject.Find("F2Button").GetComponent<Button>();
    181.         F3Button = GameObject.Find("F3Button").GetComponent<Button>();
    182.         F4Button = GameObject.Find("F4Button").GetComponent<Button>();
    183.         F5Button = GameObject.Find("F5Button").GetComponent<Button>();
    184.         F6Button = GameObject.Find("F6Button").GetComponent<Button>();
    185.         F7Button = GameObject.Find("F7Button").GetComponent<Button>();
    186.         G1Button = GameObject.Find("G1Button").GetComponent<Button>();
    187.         G2Button = GameObject.Find("G2Button").GetComponent<Button>();
    188.         G3Button = GameObject.Find("G3Button").GetComponent<Button>();
    189.         G4Button = GameObject.Find("G4Button").GetComponent<Button>();
    190.         G5Button = GameObject.Find("G5Button").GetComponent<Button>();
    191.         G6Button = GameObject.Find("G6Button").GetComponent<Button>();
    192.         G7Button = GameObject.Find("G7Button").GetComponent<Button>();
    193.         Gb1Button = GameObject.Find("Gb1Button").GetComponent<Button>();
    194.         Gb2Button = GameObject.Find("Gb2Button").GetComponent<Button>();
    195.         Gb3Button = GameObject.Find("Gb3Button").GetComponent<Button>();
    196.         Gb4Button = GameObject.Find("Gb4Button").GetComponent<Button>();
    197.         Gb5Button = GameObject.Find("Gb5Button").GetComponent<Button>();
    198.         Gb6Button = GameObject.Find("Gb6Button").GetComponent<Button>();
    199.         Gb7Button = GameObject.Find("Gb7Button").GetComponent<Button>();
    200.        
    201.        
    202.        
    203.         InputSystem.onDeviceChange += (device, change) =>
    204.         {
    205.             var midiDevice = device as Minis.MidiDevice;
    206.             if (midiDevice == null) return;
    207.  
    208.             midiDevice.onWillNoteOn += (note, velocity) =>
    209.             {
    210.  
    211.                 // When middle C (MIDI #60) is pressed:
    212.                 if (note.noteNumber == 21)
    213.                 {
    214.  
    215.                    
    216.                     ExecuteEvents.Execute(A0Button.gameObject, new PointerEventData(EventSystem.current), ExecuteEvents.submitHandler);
    217.  
    218.                 }
    219.                
    220.                 if (note.noteNumber == 22)
    221.                 {
    222.  
    223.                    
    224.                     ExecuteEvents.Execute(Bb0Button.gameObject, new PointerEventData(EventSystem.current), ExecuteEvents.submitHandler);
    225.  
    226.                 }
    227.                 if (note.noteNumber == 23)
    228.                 {
    229.  
    230.                    
    231.                     ExecuteEvents.Execute(B0Button.gameObject, new PointerEventData(EventSystem.current), ExecuteEvents.submitHandler);
    232.  
    233.                 }
     
  2. justinmed

    justinmed

    Joined:
    Dec 19, 2021
    Posts:
    106
    The script is intended to play a piano that is on the screen that is entirely made of buttons with an MIDI controller
     
  3. justinmed

    justinmed

    Joined:
    Dec 19, 2021
    Posts:
    106
    I tried giving them separate scripts that were duplicates with different namespaces and it still did not work.
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,594
    When you transition a scene usually the gameobjects (the things scripts are attached to) in that scene are deleted unless marked otherwise by calling DoNotDestroyOnLoad for said gameobject.

    Also... what is lords name is this script? Have you not heard of these things called arrays???
     
  5. justinmed

    justinmed

    Joined:
    Dec 19, 2021
    Posts:
    106
    Right, but all of the objects in each scene that are being called have the same name. All of the piano keys go by the same name in each scene A0Button, Bb0Button, and so on and so forth, I am new. I have heard of them, but did not know enough to use them if it were possible. But, I do not think that is the problem. The script should still grab each object by its name in each new scene and I think maybe it is the MIDI side of things.
     
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,594
    Just because they have the same name doesn't mean the script will survive the scene transition. Nor does it mean that the objects found by name will remain referenced when you transition to the same screen.

    2 scenes can have objects all named the same but they're still different objects. Just like how in my town there can be a Bob Smith, and in the town down the way can also have a Bob Smith.

    ...

    To properly assist you we'll need to know what the setup is of both scenes, how you transition the scenes, and if you've modified any objects to persist between scenes?
     
  7. justinmed

    justinmed

    Joined:
    Dec 19, 2021
    Posts:
    106
    Okay, so I tried putting the script into a prefab game object and put DontDestroyOnLoad(GameObject), but the script did not function when I did that. So each piano key for the corresponding not sends the player to the next scene within range of that particular key, CMajor or C Minor and so on and so forth, whatever game mode they are in and it sits within a random range of those scenes. If that helps.
     
  8. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,594
     
  9. justinmed

    justinmed

    Joined:
    Dec 19, 2021
    Posts:
    106
    I have prefabs for each scene that consist of a scoring system and main menu and gameplay helper which I put this script into an empty object and put it in those prefabs and as soon as a new scene loads it just kills the script. I had it in a prefab that also had a dont destroy on load command on it and it still killed it.
     
  10. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,390
    You really need to learn how to use arrays/collections and basic loops. I will again reiterate that you still need to learn basic C# principles because you continuously struggling due to ignoring the fundamentals. You're way out of depth here and you need to spend time to learn how things actually work instead of just writing code and hoping that it does work.

    And as mentioned your input management still makes no sense. You're subscribing to a delegate within a delegate and it is 100% not at all doing what you expect it to. We can't help you if you don't spend time to learn properly.
     
    Kurt-Dekker and CodeSmile like this.
  11. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,681
    Or a dictionary.

    Code (csharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class MIDI_InputTest : MonoBehaviour
    7. {
    8.     private Dictionary<string, Button> buttons;
    9.  
    10.     void Start()
    11.     {
    12.         ConnectButtons();
    13.     }
    14.  
    15.     void ConnectButtons()
    16.     {
    17.         string[] notes = { "A", "Ab", "B", "Bb", "C", "D", "Db", "E", "Eb", "F", "G", "Gb" };
    18.         int[] noteCounts = { 8, 7, 8, 8, 9, 7, 7, 7, 7, 7, 7, 7 };
    19.  
    20.         buttons = new Dictionary<string, Button>();
    21.  
    22.         for (int i = 0; i < notes.Length; i++)
    23.         {
    24.             for (int j = 0; j < noteCounts[i]; j++)
    25.             {
    26.                 string buttonName = notes[i] + (j + (notes[i] == "C" ? 1 : 0)) + "Button";
    27.                 buttons[buttonName] = GameObject.Find(buttonName).GetComponent<Button>();
    28.             }
    29.         }
    30.     }
    31. }

    Were the buttons in the prefab too? Because if they weren't the script may have survived but all of those button connections were lost and needed to be reattached. That's why the above script has
    ConnectButtons
    . When a scene transition is made you call that method again to reattach all of the buttons.
     
    Last edited: May 22, 2024
    CodeSmile and spiney199 like this.
  12. justinmed

    justinmed

    Joined:
    Dec 19, 2021
    Posts:
    106
    So, try and bear with me. As for Spiney, I really do not think I am going to just find all of this stuff in basic C# tutorials. And that is a very interesting method of achieving the button designation, however, it must be true, that that cannot be the issue. I only have the actual script in a prefab. The buttons are not in prefabs. When I look at them live in the hierarchy after a scene has loaded it shows all of the buttons are plugged in where they should be. I think that it has to be something to do with the MIDI. Thank you for all the help, I do appreciate it.
     
  13. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,922
    Why are you changing the scene in the first place?

    I figure it could very well be done with a single scene. If you need to show some fullscreen GUI, have that canvas object in the scene and disable it, then enable it when you need it. You can avoid most of the "change scene" issues that way.

    You could also use additive scene loading - load a scene with the GUI in it additively, then unload it as needed. That way the rest of the scene also remains as is.

    This is where AI can be really helpful because you can just ask. ;)

    Btw, Unity has it's own AI chat which provides better answers. You can trial it for two weeks so be sure to make the most of the free time.
     
  14. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,390
    It's not about finding what you're specifically try to do in a tutorial. It's about giving yourself the tools to solve each development challenge as they arise. You're throwing yourself in with no tools, and hit constant road blocks as a result.

    We give you solutions, but you seldom are ever able to apply them as you don't have a grip on the core fundamentals.

    I think this is bad advice honestly. Using chat bots for programming only really works if you understand programming in the first place. What OP needs is do a good course on C#.
     
    Kurt-Dekker likes this.
  15. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,922
    I meant asking AI things like "what is a variable in C#" and what I put in the spoiler. ;)
    Using variables in your code offers numerous benefits that improve readability, maintainability, and efficiency. Here are some of the key advantages:

    1. Readability
    Variables make your code more readable and understandable by giving meaningful names to data values. This helps others (and yourself) understand the purpose of specific data in the code.

    Example:
    Without variables:

    csharp
    Console.WriteLine(3.14159 * 2 * 5); // What does 3.14159 and 5 represent?

    With variables:

    csharp
    double pi = 3.14159;
    double radius = 5;
    Console.WriteLine(pi * 2 * radius); // Clear that we are calculating the circumference of a circle

    2. Reusability
    Variables allow you to reuse values throughout your code without having to repeatedly write the same value.

    Example:
    csharp
    double radius = 5;
    double circumference = 2 * Math.PI * radius;
    double area = Math.PI * radius * radius;

    Console.WriteLine("Circumference: " + circumference);
    Console.WriteLine("Area: " + area);

    3. Maintainability
    Variables make your code easier to maintain and modify. If a value needs to be changed, you only have to update it in one place.

    Example:
    Without variables:

    csharp
    Console.WriteLine(2 * 3.14159 * 5);
    Console.WriteLine(3.14159 * 5 * 5);

    With variables:

    csharp
    double radius = 5;
    double pi = 3.14159;

    Console.WriteLine(2 * pi * radius);
    Console.WriteLine(pi * radius * radius);

    If you need to change the radius, you only change it in one place.

    4. Debugging
    Variables help in debugging by allowing you to inspect and modify values during the execution of your program. You can set breakpoints and watch specific variables to understand how values change over time.

    5. Avoiding Magic Numbers
    Using variables helps eliminate "magic numbers" (hard-coded values) in your code, making it more self-explanatory and less error-prone.

    Example:
    Without variables:

    csharp
    if (age > 18) {
    // Some logic
    }

    With variables:

    csharp
    int legalAge = 18;
    if (age > legalAge) {
    // Some logic
    }

    6. Parameterization
    Variables allow you to write more generic and flexible code that can handle different inputs and scenarios.

    Example:
    csharp
    void PrintArea(double radius) {
    double area = Math.PI * radius * radius;
    Console.WriteLine("Area: " + area);
    }

    PrintArea(5);
    PrintArea(10);

    7. Logical Grouping
    Variables enable you to logically group related data, making it easier to understand the relationships between different parts of your code.

    Example:
    csharp
    string firstName = "John";
    string lastName = "Doe";
    int age = 30;

    Console.WriteLine("Name: " + firstName + " " + lastName + ", Age: " + age);

    Summary
    Using variables improves code quality by:

    • Enhancing readability
    • Increasing reusability
    • Simplifying maintenance
    • Aiding in debugging
    • Eliminating magic numbers
    • Allowing for parameterization
    • Enabling logical grouping of data
    These benefits contribute to writing cleaner, more efficient, and maintainable code.

    I know most would ask "how to implement feature x" - that indeed is often ripe with issues when you don't understand the code handed to you.
     
  16. justinmed

    justinmed

    Joined:
    Dec 19, 2021
    Posts:
    106
    OMG! Thank you for this suggestion! With the help of Muse I was able to prevent my MIDI from dying on scene change. It turned out to be something with buttons and suggested something along the lines of this but not exactly and I figured to put it over every buttons assignment to search for the named object in the scene and it worked! The chat bot is a lot nicer too! It is a piano sight reading learning improvement assistance application and I wanted to have modes where you can use the mouse, but I wanted those modes to be compatible with MIDI as well and the game modes consist of notes being displayed that correspond to a UI button keyboard on the screen that the player has to match the correct key to the note that is being displayed on the Staff for the score to go up and because with a mouse cursor you onlly have one input and it is 88 to 1 rather than just 88 to 88 switching I could not find any other way than to go over scenes rather than by an index within one scene. And it works fine, the user has no idea.
    Code (CSharp):
    1. SceneManager.sceneLoaded += (arg0, mode) =>
    2.         A1Button = GameObject.Find("A1Button").GetComponent<Button>();
     
    CodeSmile likes this.